async function takeAndUploadPhotoAsync() {
// Display the camera to the user and wait for them to take a photo or to cancel
// the action
let result = await ImagePicker.launchCameraAsync({
allowsEditing: true,
aspect: [4, 3],
});
if (result.cancelled) {
return;
}
// ImagePicker saves the taken photo to disk and returns a local URI to it
let localUri = result.uri;
let filename = localUri.split('/').pop();
// Infer the type of the image
let match = /\.(\w+)$/.exec(filename);
let type = match ? `image/${match[1]}` : `image`;
// Upload the image using the fetch and FormData APIs
let formData = new FormData();
// Assume "photo" is the name of the form field the server expects
formData.append('photo', { uri: localUri, name: filename, type });
return await fetch(YOUR_SERVER_URL, {
method: 'POST',
body: formData,
headers: {
'content-type': 'multipart/form-data',
},
});
}
async function takePhotoAndUpload() {
let result = await ImagePicker.launchCameraAsync({
allowsEditing: false, // higher res on iOS
aspect: [4, 3],
});
if (result.cancelled) {
return;
}
let localUri = result.uri;
let filename = localUri.split('/').pop();
let match = /\.(\w+)$/.exec(filename);
let type = match ? `image/${match[1]}` : `image`;
let formData = new FormData();
formData.append('photo', { uri: localUri, name: filename, type });
return await fetch('http://example.com/upload.php', {
method: 'POST',
body: formData,
header: {
'content-type': 'multipart/form-data',
},
});
}
const blobToBase64 = blob => {
const reader = new FileReader();
reader.readAsDataURL(blob);
return new Promise(resolve => {
reader.onloadend = () => {
resolve(reader.result);
};
});
};
const formData = new FormData();
const base64 = await blobToBase64(blob);
formData.append('file', base64);
formData.append('data', JSON.stringify(payload)); // additional data (I parse the string as json in the backend to get my payload back)
// same code as chosen answer, this was not part of the problem
return await fetch(YOUR_SERVER_URL, {
method: 'POST',
body: formData,
headers: {
'content-type': 'multipart/form-data',
},
});
async function uploadImage(uploadFile){
const data = new FormData()
data.append('name',{
name: image_name, {/* name your image whatever you want*/}
type: 'image/jpeg', {/* type of image that you're uploading*/}
uri: uploadFile {/*data, file or image from ImagePicker here you should pass uri data but not all data from ImagePicker*/}
})
{/*names of data object should be like this: name, type, uri*/}
const response = await fetch(my_upload_api.php,{
method: 'POST',
body: data,
headers: {
'Content-Type': 'multipart/form-data'
}
})
}
6条答案
按热度按时间nukf8bse1#
使用Expo
ImagePicker
API显示相机或相机胶卷,并获取有关所选图像的信息:有关包含服务器代码的更全面的示例,请参阅下面的repo:https://github.com/expo/image-upload-example。
lsmd5eda2#
官方示例使用Node.js,以下是使用PHP的方式:
世博会
upload.php
bz4sfanl3#
wf82jlnq4#
对于Expo,除了使用Expo文件系统uploadAsync之外,没有什么对我有用
注意- imageUri格式为file:///mypath/to/image.png
goqiplq25#
由于所选的解决方案实际上对我不起作用,下面是我如何使用Expo和Django Rest Framework作为后端进行文件上传。
在Django中,我可以使用自定义解析器将base64字符串解码为字节,然后使用它创建一个
SimpleUploadedFile
对象。mfpqipee6#
对于那些没有找到或解决这个问题的人来说。我花了三天的时间来寻找解决方案,我得到了它。在我的例子中,它是数据对象的命名元素。Android版本10,Expo 4.11.0,
这里是前线
这是PHP的后台
}
这是我第一次尝试寻找解决方案的博客。如果解决方案在这里,我可能会花一天或更少的时间来解决这个问题。我希望我能帮助到某人。