React native 如何使用Axios将本地设备映像上载到S3存储桶
我需要上传一个图像直接到S3桶。我正在使用react-native和react-native图像选择器来选择照片。这将返回一个本地图像uri。这是我的密码React native 如何使用Axios将本地设备映像上载到S3存储桶,react-native,axios,image-uploading,react-native-image-picker,React Native,Axios,Image Uploading,React Native Image Picker,我需要上传一个图像直接到S3桶。我正在使用react-native和react-native图像选择器来选择照片。这将返回一个本地图像uri。这是我的密码 ImagePicker.showImagePicker(options, response => { var bodyFormData = new FormData(); // If I don't use FormData I end up //
ImagePicker.showImagePicker(options, response => {
var bodyFormData = new FormData(); // If I don't use FormData I end up
// uploading the json not an image
bodyFormData.append('image', {
uri: response.uri, // uri rather than data to avoid loading into memory
type: 'image/jpeg'
});
const uploadImageRequest = {
method: 'PUT',
url: presignedS3Url,
body: bodyFormData,
headers: {
'Content-Type: 'multipart/form-data'
}
};
axios(uploadImageRequest);
});
这几乎奏效了。。当我检查我的S3存储桶时,我有一个几乎是图像的文件。它的格式如下
--Y_kogEdJ16jhDUS9qhn.KjyYACKZGEw0gO-8vPw3BcdOMIrqVtmXsdJOLPl6nKFDJmLpvj^M
content-disposition: form-data; name="image"^M
content-type: image/jpeg^M
^M
<Image data>
如果我手动进入并删除标题,那么我有我的图像!但是,我需要将一个图像直接上传到S3,客户机将获取并期望它已经具有正确的图像格式
我可以使用response.data和对字符串进行解码并直接上传,但为了内存起见,我不希望这样做。使用带有预先签名URL的AJAX将图像从客户端上传到S3
你已经有一段时间没有发布你的问题了,所以我想你已经找到了一个解决方案,但无论如何。。。我也试着这么做,即使用axios将图像上传到S3,但我就是无法使其正常工作。幸运的是,我发现我们可以很容易地用普通AJAX实现这一点:
const xhr = new XMLHttpRequest();
xhr.open('PUT', presignedS3Url);
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
console.log('Image successfully uploaded to S3');
} else {
console.log('Error while sending the image to S3. Status:', xhr.status);
}
}
}
xhr.setRequestHeader('Content-Type', 'image/jpeg');
xhr.send({ uri: imageUri, type: 'image/jpeg', name: fileName});
此代码取自