我正在使用 React Native 和 expo,并尝试通过 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:网络请求失败]”。任何想法?
谢谢之前
最后我找到了解决方案。
我不知道为什么react-native expo应用程序不支持获取blob。但您可以使用以下方法替换 blob:
form.append('file', { uri, name: 'media', type: `image/${type}` } as any)
输入 3 个参数很重要,以避免 android 和 ios 出现错误。
就我而言,我的最终解决方案如下所示:
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)
})
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)