-1

I am trying to get a document file from phone storage when button is pressed and than upload it server. But i don't know which library to use and how to do it.

AstroCB
  • 12,337
  • 20
  • 57
  • 73
endari
  • 1
  • 1
  • It would be helpful if you ask a specific question and show what you have done to solve it so far. – Rohit Saxena Jun 29 '17 at 16:39
  • Well I have done only the designe. I have 2 buttons and a textview. First button is attach so when I press it it opens phone storage to pick up a pdf file and shows the document name in TextView. The second button is Save that saves that fthrought post api. – endari Jun 30 '17 at 14:28

1 Answers1

0

If you are willing to use a library you have both React-native-fetch-blob or axios.

If React-native-fetch-blob you can do it like this:

RNFetchBlob.fetch('POST', 'http://www.example.com/upload-form', {
    Authorization : "Bearer access-token",
    otherHeader : "foo",
    'Content-Type' : 'multipart/form-data',
  }, [
    // element with property `filename` will be transformed into `file` in form data
    { name : 'avatar', filename : 'avatar.png', data: binaryDataInBase64},
    // custom content type
    { name : 'avatar-png', filename : 'avatar-png.png', type:'image/png', data: binaryDataInBase64},
    // part file from storage
    { name : 'avatar-foo', filename : 'avatar-foo.png', type:'image/foo', data: RNFetchBlob.wrap(path_to_a_file)},
    // elements without property `filename` will be sent as plain text
    { name : 'name', data : 'user'},
    { name : 'info', data : JSON.stringify({
      mail : 'example@example.com',
      tel : '12345678'
    })},
  ]).then((resp) => {
    // ...
  }).catch((err) => {
    // ...
  })

You will hae to manage to get the file path from libraries like RNFS or even RNFetch blob.

https://github.com/wkh237/react-native-fetch-blob

You can use axios too (https://github.com/mzabriskie/axios), but I don't use it so I can't help you any further.

The difference between both is the way they send the data. RNFB uses the fetch api and goes down to native to get the Base64 encoding, axios works over XMLHttpRequests, which is more likely to be used in internet browsers.

Hope it helps.

EnriqueDev
  • 1,207
  • 2
  • 12
  • 25