我正在嘗試使用react-native-image-picker和預簽名的 URL 從 android 上傳影像,而是獲取沒有內容的影像,如果我將影像檔案擴展名更改為 JSON,我將獲得 JSON 格式的 blob 內容,如下所示
{"_data":{"lastModified":0,"name":"rn_image_picker_lib_temp_e47f0064-c49c-439e-95e2-30ed167d1444.jpg","size":2447490,"offset":0,"type":"image/jpeg","blobId":"8d905790-3113-4cb7-b9ab-b5415e5e6b50","__collector":{}}}
下面是 Axios 請求和組件的代碼片段
// HTTP Requests
export const save = async (uri, data, type) =>
await axios.put(uri, data, {headers: {'Content-Type': type, Accept: "application/json"}})
.then(response => {
return response.data;
}, error => {
return error.response.data;
});
export const getImageURI = async id =>
await axios.post(URL id)
.then(response => {
return response.data;
}, error => {
return error.response.data;
});
import React from 'react';
import { TouchableOpacity } from 'react-native';
import { launchImageLibrary } from 'react-native-image-picker';
import { getImageURI, save } from '../Services';
const UploadImage = (props) => {
const uploadImageOnS3 = async (file) => {
const res = await fetch(file.uri);
const blob = await res.blob();
let signedUriResp = await getImageURI("someId");
await save(signedUriResp.url, blob, file.type);
}
return (
<TouchableOpacity onPress={() =>
launchImageLibrary({mediaType: 'photo', includeBase64: true},
(response) => uploadImageOnS3(response))}
/>
);
}
export default UploadImage;
版本:
"react-native-image-picker": "3.2.1",
"react-native": "0.63.4",
"react": "16.13.1",
"axios": "^0.21.1",
我已將相機和存盤權限授予 android 中的應用程式。不確定為什么 blob 沒有作為影像正確上傳到 S3。有沒有人遇到過類似的問題?請讓我知道是否有任何我遺漏的東西。
uj5u.com熱心網友回復:
在保存影像呼叫中更改axios為fetch解決了該問題。這是上述方法的片段
export const save = async (data, uri, type) =>
await fetch(uri, {
method: 'PUT',
headers: {
Accept: 'application/json',
'Content-Type': type
},
body: data
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/416573.html
標籤:
