I'm in a tough spot here. I can't figure out what's going wrong. My goal is to send a file using the expo-image-picker component to the server. The form gets sent, but the image doesn't. When I use the fetch
command, I immediately get a "Network request failed" error. The server receives the request, but without the image attached.
Additional details:
I've tried creating a form-data object and appending the blob to it. I also attempted using
as suggested in many articles, ignoring the TypeScript error, but it failed too.FormData.append("image", {uri, name: 'filename', type: 'image/filetype'})
I'm not utilizing AWS or Firebase, so I'm not using those libraries. I don't see a significant difference in how they handle it compared to my approach.
I haven't defined any specific permissions for this process. I came across some discussions about permissions for uploads, but the information seemed outdated, referencing times before Android 5.0.
Below are the functions I'm using for the submission, with pathToImage
being returned from the ImagePicker.
const fetchImageFromUri = async (uri: string) => {
try {
const response = await fetch(uri);
const blob = await response.blob();
return blob;
} catch (error) {
console.log("fetchImageFromUri error:", error);
throw new Error("fetchImageFromUri");
}
};
const upload = async () => {
setMessage("");
setErrMessage("");
if (pathToImage != null) {
const fileToUpload = await fetchImageFromUri(pathToImage);
const formData = new FormData();
formData.append("action", "Image Upload");
formData.append("image", fileToUpload, "filename");
// Rest of the upload function goes here
You can access the complete code in my GitHub repository.