This website collects cookies to deliver better user experience
Uploading files in react native
Uploading files in react native
I've been working with react native for one year now, and I really love it.
Today was the first time I had to upload files to our back-end server.
Normally when I encounter something I don't know my first instinct is to search for it in the official documentations unfortunately I couldn't find anything helpful.
Without using third party library:
Prepare file as form data:
If you picked a file using document picker,image picker or camera and you pass it down as file object.
Why to use this library if I can do it without library?
I would say the main reason is to have the ability to show determinate progress indicator using the progress callback
import { uploadFiles, DocumentDirectoryPath } from "react-native-fs";
var files = [
{
name: "file",
filename: "file.jpg",
filepath: DocumentDirectoryPath + "/file.jpg",
filetype: "image/jpeg",
},
];
uploadFiles({
toUrl: "https://upload-service-url",
files: files,
method: "POST",
headers: {
Accept: "application/json",
},
//invoked when the uploading starts.
begin: () => {},
// You can use this callback to show a progress indicator.
progress: ({ totalBytesSent, totalBytesExpectedToSend }) => {},
});
Important note:
If you are using react native debugger to try inspecting network requests and you get errors from back-end service.
It is because currently the debugger does'nt suppot the formData as mentioned in there documentations under 'Limitations' react-native-debugger