簡體   English   中英

React-Native expo POST Blob

[英]React-Native expo POST Blob

我正在使用 react native 和 expo,我正在嘗試通過獲取 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)
})

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM