document-picker icon indicating copy to clipboard operation
document-picker copied to clipboard

File Upload via fetch

Open schmaluk opened this issue 9 years ago • 6 comments

How can I make file Upload work with react-native-document-picker:


DocumentPicker.show({
      filetype: [DocumentPickerUtil.allFiles()],
    }, (error, res) => {
     const { uri, type: mimeType, fileName } = res;

      const formData = new FormData();
      formData.append('file', { uri, type: mimeType, name: fileName });

      const config = {
            method: 'POST',
            headers: {Accept: 'application/json'},
            body: formData,
      };
      console.log(config);
      return fetch("/endpoint", config).then((response) => { console.log(response); response.json(); });

});

But get back that file is missing in upload on the server

schmaluk avatar Sep 27 '17 14:09 schmaluk

Thanks @mlukasch you save my day.

tony-orn avatar Nov 21 '17 09:11 tony-orn

@mlukasch Thanks this actually works!

rogerarce avatar Apr 19 '18 12:04 rogerarce

@mlukasch How do you send a file with other data ? (like publication date, etc.)

ivanherve avatar Jun 16 '18 11:06 ivanherve

Thank you @schmaluk

AlanQuadros avatar Mar 21 '19 00:03 AlanQuadros

@schmaluk you have no IDEA, you basically saved my life !!

zsubzwary avatar Jun 11 '19 05:06 zsubzwary

This works only for iOS, for Android you get a uri form the ContentProvider of type content:// which FormData can't read

ionbaratol avatar Dec 16 '20 12:12 ionbaratol