3

I have my video file assets-library://asset/asset.mov?id=766BDDA3-F0EB-43B3-B719-4EA851692B91&ext=mov and I am trying to now upload it to my Express server.

const uri = 'http://localhost:3000/upload';
const formData = new FormData();
formData.append('file', 'assets-library://asset/asset.mov?id=766BDDA3-F0EB-43B3-B719-4EA851692B91&ext=mov');

fetch(uri, {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'multipart/form-data boundary=gc0p4Jq0M2Yt08jU534c0p'
    },
    body: formData
  })
    .then(res => {
      console.log({ res });
    })
    .catch(err => {
      console.log(err);
    });

My Express Server API endpoint:

app.post('/upload', upload.single('file'), (req, res) => {
  console.log(req.file);
  res.send('Done');
});

The console.log(req.file) returns undefined.

Do I need to do an extra step in between?

As per Gavin's comment, I tried out RNFetchBlob.

RNFetchBlob.fetch(
    'POST',
    'http://localhost:3000/upload',
    {
      'Content-Type': 'multipart/form-data'
    },
    [
      {
        name: 'file',
        filename: 'vid.mov',
        data: RNFetchBlob.wrap(file)
      }
    ]
  )
    .then(res => {
      console.log(res);
    })
    .catch(err => {
      console.log(err);
    });

My application crashes without any logs on Xcode or in the Debugger.

Dan
  • 8,041
  • 8
  • 41
  • 72
  • https://github.com/wkh237/react-native-fetch-blob React-Native needs a package like this to convert it into a blob – Gavin Thomas Dec 26 '17 at 17:26
  • Thanks for the suggestion @GavinThomas do you know which specific part of the README shows how to convert it? – Dan Dec 26 '17 at 17:36
  • https://stackoverflow.com/questions/47451507/react-native-upload-video-with-fetch-blob-and-image-picker In this example they give the full code to pick a video from the Image Picker in react native. If you ARENT doing this, you would just need to start at RNFetchBlob.fetch() – Gavin Thomas Dec 26 '17 at 18:07
  • RN Fetch Blob is all new to me also, So i can't provide any more info than that and what I did. But I used photos. I do know you have to use this package, or encode your data yourself because RN doesn't support blobs natively. Heres a link where I used it to upload photos to firebase https://github.com/GavinThomas1192/motoMechanicMeeKanic/blob/master/App/Actions/vehicle-actions.js – Gavin Thomas Dec 26 '17 at 18:10
  • 1
    Got it working thanks @GavinThomas - I edited my answer and App Crashes was due to not having a property set in Info.plist. – Dan Dec 27 '17 at 10:54

0 Answers0