I'm trying to upload a image from react native expo-image to my ColdFusion server.
My react native component:
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>
...
Then, in my action file I send to my .cfc file using axis
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
...
In my .cfc file, I try to upload using fileUpload (CFScript):
destination = expandPath("./myFolder/");
if(!directoryExists(destination)){
directoryCreate(destination);
}
fileUpload(destination, arguments.File, "*", "MakeUnique" );
When I run it, the arguments.File contains:
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
However, the ColdFusion .cfc returns the error:
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.
How can I upload a file from expo-image to the server?
Thank you.
I found the problem. We need to add the uri to the forData:
const imgName = 'myIMG.png';
formData.append('test', { uri: obj.image, name: imgName });
``