gpt4 book ai didi

javascript - React-Native 博览会 POST Blob

转载 作者:行者123 更新时间:2023-12-03 16:26:07 24 4
gpt4 key购买 nike

我正在使用带有 expo 的 react native,我正在尝试通过 fetch api 发布 blob 图像。我正在为正文使用表单数据格式,并且我有下一个代码:

       const blob = await response.blob()
const form = new FormData()
form.append('file', blob)
const options: RequestInit = {
method: 'POST',
headers,
body: form
}
return this.fetch(path, options).then(res => {
console.log("FETCHING", res.status)
this.processResponse(path, options, res)
}).catch(err => {
console.log("FETCH ERROR", err)
})
响应永远不会发生,我的控制台显示“FETCH ERROR [TypeError: Network request failed]”。任何想法?
事先感谢

最佳答案

最后我找到了一个灵魂。
我不知道为什么 react-natvie expo 应用程序不支持获取 blob。但是您可以使用以下方法替换 blob:

form.append('file', { uri, name: 'media', type: `image/${type}` } as any)
为了避免android和ios的错误,放置3个参数很重要。
就我而言,我的最终解决方案如下所示:
async postMedia(path: string, uri: string) {
let type = uri.substring(uri.lastIndexOf(".") + 1);
const headers = await this.getHeaders('multipart/form-data')
const form = new FormData()
form.append('file', { uri, name: 'media', type: `image/${type}` } as any)
const options: RequestInit = {
method: 'POST',
headers,
body: form
}
return this.fetch(path, options).then(res => {
console.log("FETCH MEDIA", res)
this.processResponse(path, options, res)
}).catch(err => {
console.log("FETCH ERROR", err)
})
}

关于javascript - React-Native 博览会 POST Blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66372873/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com