如何在react-native中上传带有表单数据的图像或pdf文件?

jeh*_*hoi 4 reactjs react-native

我找到了一些库,但我不确定哪一个适合我并且稳定。

我正在使用表单数据实现类似电子邮件附件的功能。

选择从图库上传文件或 Android 设备上的文件管理并上传到服务器。

你能推荐一些不错的图书馆吗?谢谢 !

我发现了什么

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

等等..

Ris*_*waj 6

您需要首先使用文档选择器 - 使用
react-native-document-picker https://github.com/Elyx0/react-native-document-picker 然后在文档选择器的回调中创建一个表单数据对象

data = new FormData();

data.append('resource', {
    name: res.fileName,
    uri: res.uri, type: res.type
});
Run Code Online (Sandbox Code Playgroud)
var req = {
  method: "POST",
  headers: {
    'Auth-Identifier': access_token,
  },
  body:file
}
Run Code Online (Sandbox Code Playgroud)

在您的 fetch 调用中使用上述请求。它会对你有用。

  • Android 上的 React Native 返回的 uri 是一个“content://”——来自内容提供者的 uri,而 FormData 不知道如何读取它。对于这种情况有什么解决方法吗? (2认同)