【发布时间】:2021-09-28 18:36:16
【问题描述】:
我在上传文件到服务器时遇到了一些麻烦,因为我找不到将 img 转换为二进制数据结构的方法。
// upload function
const UploadImg = async (img: any) => {
// img from react-native-image-crop-picker
const fromData = new FormData();
fromData.append('file', `${img.data}`);
return fetch(`${API_URL_R}/upload`, {
method: 'POST',
body: fromData, // this should be binary
})
};
// use
// import ImagePicker from 'react-native-image-crop-picker'
ImagePicker.openPicker({
width: 300,
height: 400,
cropping: true,
includeBase64: true,
}).then(async image => {
await UploadImg(image);
});
【问题讨论】:
-
您可以将文件作为Base64发送到服务器。
标签: reactjs react-native react-native-android blob fetch-api