我正在尝试将图像从react native expo-image上传到我的ColdFusion服务器。
我的本机组件
:import * as ImagePicker from 'expo-image-picker';
...
_pickImage = async () => {
const result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
allowsEditing: true,
aspect: [4, 3],
});
if (!result.cancelled) {
this.setState({ image: result.uri });
}
};
_takePicture = async () => {
const result = await ImagePicker.launchCameraAsync({
allowsEditing: true,
aspect: [4, 3],
});
if (!result.cancelled) {
this.setState({ image: result.uri });
}
}
...
<View style={{ flexDirection: 'row', flexWrap: 'wrap' }}>
<Button onPress={this._pickImage}>
<Text>Select a Image</Text>
</Button>
<Button onPress={this._takePicture}>
<Text>Take a Picture</Text>
</Button>
</View>
...
然后,在我的操作文件中,我使用轴发送到.cfc文件
export const addWO = (obj) => {
return (dispatch) => {
const formData = new FormData();
formData.append('file', obj.image);
console.log('>> formData >> ', formData);
axios.post('http://myServer/MyCFC.cfc?method=myMethod',
formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
}
).then
...
在我的.cfc文件中,我尝试使用fileUpload(CFScript)上传:
destination = expandPath("./myFolder/");
if(!directoryExists(destination)){
directoryCreate(destination);
}
fileUpload(destination, arguments.File, "*", "MakeUnique" );
运行时,arguments.File包含:
file:///Users/myUser/Library/Developer/CoreSimulator/Devices/AE9B608B-3FFB-4C72-8971-C14D56E978F7/data/Containers/Data/Application/116E6C9C-B7EA-429B-8C4A-D89AF777BAA6/Library/Caches/ExponentExperienceData/%2540anonymous%252FiRentApp-5f186599-13ec-4736-b728-f84288e5a0cc/ImagePicker/3762AF23-5F01-453D-B257-E1FB25832994.jpg
但是,ColdFusion .cfc返回错误:
The form field file:///Users/myUser/Library/Developer/CoreSimulator/Devices/AE9B608B-3FFB-4C72-8971-C14D56E978F7/data/Containers/Data/Application/116E6C9C-B7EA-429B-8C4A-D89AF777BAA6/Library/Caches/ExponentExperienceData/%2540anonymous%252FiRentApp-5f186599-13ec-4736-b728-f84288e5a0cc/ImagePicker/3762AF23-5F01-453D-B257-E1FB25832994.jpg did not contain a file.
如何将文件从expo-image上传到服务器?
谢谢。
我发现了问题。我们需要将uri添加到forData中:
const imgName = 'myIMG.png';
formData.append('test', { uri: obj.image, name: imgName });
``
答案 0 :(得分:1)
我想出了解决方案。我们需要将URI添加到FormData中。 我还包括一个名字
const formData = new FormData();
const imgName = 'myIMG.png';
formData.append('test', { uri: obj.image, name: imgName });
axios.post('http://myServer/MyCFC?method=myMethod',
formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
}
).then
...
...,并将CFC上传代码更改为:
fileUpload(destination, "test", "*", "MakeUnique" );