Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 从手机中选择文档文件并上传到服务器_Node.js_Reactjs_React Native_React Native Fetch Blob - Fatal编程技术网

Node.js 从手机中选择文档文件并上传到服务器

Node.js 从手机中选择文档文件并上传到服务器,node.js,reactjs,react-native,react-native-fetch-blob,Node.js,Reactjs,React Native,React Native Fetch Blob,当按下按钮时,我试图从手机存储器中获取一个文档文件,然后将其上传到服务器。但我不知道该使用哪个库以及如何使用。如果您愿意使用库,您可以使用React native fetch blob或axios 如果是React native fetch blob,您可以这样做: RNFetchBlob.fetch('POST', 'http://www.example.com/upload-form', { Authorization : "Bearer access-token", oth

当按下按钮时,我试图从手机存储器中获取一个文档文件,然后将其上传到服务器。但我不知道该使用哪个库以及如何使用。

如果您愿意使用库,您可以使用React native fetch blob或axios

如果是React native fetch blob,您可以这样做:

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) => {
    // ...
  })
您必须设法从诸如RNFS甚至RNFetch blob之类的库中获取文件路径

您也可以使用axios(),但我不使用它,因此无法进一步帮助您

两者的区别在于它们发送数据的方式。RNFB使用fetch api并转到本机以获得Base64编码,axios通过XMLHttpRequests工作,后者更可能用于internet浏览器


希望能有所帮助。

如果你问一个具体的问题,并展示你迄今为止为解决这个问题所做的工作,那会很有帮助。我只做了设计。我有两个按钮和一个文本视图。第一个按钮是attach,所以当我按下它时,它会打开手机存储区以获取pdf文件,并在TextView中显示文件名。第二个按钮是Save,它保存fthround post api。谢谢helpful@EnriqueDev你能检查一下这个问题吗,看看你能不能帮忙!谢谢