react-native android 获取 Blob JSON 而不是 S3 预签名 URL 中的图像文件
react-native android getting Blob JSON instead of image file in S3 pre-signed URL
我正在尝试使用 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。有没有人遇到过类似的问题?请让我知道是否有任何遗漏。
在保存图像调用中将 axios
更改为 fetch
解决了这个问题。这是上述方法的片段
export const save = async (data, uri, type) =>
await fetch(uri, {
method: 'PUT',
headers: {
Accept: 'application/json',
'Content-Type': type
},
body: data
});
我正在尝试使用 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。有没有人遇到过类似的问题?请让我知道是否有任何遗漏。
在保存图像调用中将 axios
更改为 fetch
解决了这个问题。这是上述方法的片段
export const save = async (data, uri, type) =>
await fetch(uri, {
method: 'PUT',
headers: {
Accept: 'application/json',
'Content-Type': type
},
body: data
});