how to upload image or pdf file with form-data in react-native?

为君一笑 提交于 2019-12-08 11:29:23

问题


i found some libraries but i am not sure which one is suitable and stable for me.

i am implementing like email attachment with form-data.

Choose uploading files from gallery or file Manage on Android device and upload to server.

could you recommend nice library? thank you !

what i have found

https://github.com/kaancelen/react-native-nononsense-file-picker

https://github.com/Elyx0/react-native-document-picker

https://github.com/farmisen/react-native-file-uploader

https://github.com/huangzuizui/react-native-file-opener

and so on..


回答1:


You need to first use a document picker - use
react-native-document-picker https://github.com/Elyx0/react-native-document-picker Then create a form data object in callback of document picker

data = new FormData();

                data.append('resource', {
                    name: res.fileName,
                    uri: res.uri, type: res.type
                });

var req = {
  method: "POST",
  headers: {

    'Auth-Identifier': access_token,

  },
  body:file
}

use above request in your fetch call. It will work for you.



来源:https://stackoverflow.com/questions/49605686/how-to-upload-image-or-pdf-file-with-form-data-in-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!