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

How To Upload File Using Axios?

Open loverdeveloper opened this issue 8 years ago • 9 comments

Can i use FormData and append to upload this?

loverdeveloper avatar Nov 13 '18 09:11 loverdeveloper

Yeah, I've used the following to upload using axios and redux-form. fields is the data from the redux form and the uploadFile is the res returned from RN-Document-Picker.

Format Data

    const formatData = ({fields, uploadFile}) => {
      const formData = new FormData();

      Object.keys(fields).forEach(key => {
        formData.append(key, fields[key]);
      });

      formData.append(
        'file',
        uploadFile
      );

      return formData;
    };

Upload formData

    const formData = formatData(response.data.upload_form.fields, uploadFile);
        return axios
          .create({
            baseURL: uploadUrl,
            timeout: 600000, // 10 Minutes
            method: 'POST',
            data: formData,
            // Add any further headers here such as auth
          })
          .request()
          .then(data => {
            dispatch({
              type: UPLOAD.SUCCESS,
              payload: data,
            });
          })
          .catch(errors => {
            dispatch({
              type: UPLOAD.FAILURE,
              payload: errors,
            });
          });

phil-hodgson-1st avatar Nov 13 '18 22:11 phil-hodgson-1st

@phil-hodgson-1st i'm not use redux , how can upload without redux? Thanks , Thanks , Thanks

loverdeveloper avatar Nov 14 '18 07:11 loverdeveloper

You should be able to just remove the dispatches and action type/payloads so the formatData remains the same. The second function changes to this:

        const formData = formatData(response.data.upload_form.fields, uploadFile);
        return axios
          .create({
            baseURL: uploadUrl,
            timeout: 600000, // 10 Minutes
            method: 'POST',
            data: formData,
            // Add any further headers here such as auth
          })
          .request()
          .then(data => {
            // Call a function here if the request is successful
          })
          .catch(errors => {
            // Call a function here if the request fails
          });

Just move in your own functions here for success and fail 👍

phil-hodgson-1st avatar Nov 18 '18 20:11 phil-hodgson-1st

@phil-hodgson-1st Thank you for the example!

What does upload file mean in this case? is it any kind of file (image, pdf) and is upload the uri? or the actual raw data?

sorinfratila avatar Dec 07 '18 07:12 sorinfratila

getting this error -->>

Error: Network Error at createError (createError.js:16) at XMLHttpRequest.handleError (xhr.js:81) at XMLHttpRequest.dispatchEvent (event-target-shim.js:818) at XMLHttpRequest.setReadyState (XMLHttpRequest.js:574) at XMLHttpRequest.__didCompleteResponse (XMLHttpRequest.js:388) at XMLHttpRequest.js:501 at RCTDeviceEventEmitter.emit (EventEmitter.js:189) at MessageQueue.__callFunction (MessageQueue.js:395) at MessageQueue.js:106 at MessageQueue.__guard (MessageQueue.js:343)

ayansGit avatar Sep 10 '19 07:09 ayansGit

Are there any updates?

arthedza avatar Jun 09 '20 13:06 arthedza

Try "Content-Type": "application/x-www-form-urlencoded", Accept: "application/json"

bharathnallamothu avatar Jul 09 '20 07:07 bharathnallamothu

I also get the same error Error: Network Error any update

shivani2911 avatar Nov 05 '20 05:11 shivani2911

I also get the same error Error: Network Error any update

try to see this https://github.com/rnmods/react-native-document-picker/issues/357#issuecomment-728983990

irsyaadbp avatar Nov 17 '20 15:11 irsyaadbp