将本机上传图像作为文件进行反应

时间:2017-08-28 07:41:19

标签: image file reactjs react-native upload

我无法从我的本机应用上传图片。我有一个反应应用程序,使用<input>作为文件将图像上传到后端。

网络应用程序上传到后端,如

File {name: "413_loveis_takingcareofu_wm.jpg", lastModified: 1503889026000, lastModifiedDate: Mon Aug 28 2017 10:57:06 GMT+0800 (HKT), webkitRelativePath: "", size: 34534, …}
lastModified
:
1503889026000
lastModifiedDate
:
Mon Aug 28 2017 10:57:06 GMT+0800 (HKT) {}
name
:
"413_loveis_takingcareofu_wm.jpg"
size
:
34534
type
:
"image/jpeg"

我使用react-native-image-picker在React Native移动应用上获取图像,我有base64和filepath。

如何从

转换文件路径
"file:///Users/kel/Library/Developer/CoreSimulator/Devices/7FAC36F6-803A-4268-9D65-28A9B8B65636/data/Containers/Data/Application/E34DD4A1-0207-4365-A428-614D39785781/Documents/images/798807FA-D41E-4109-B813-D21F22CC7F0D.jpg"

什么适用于网络应用程序?

2 个答案:

答案 0 :(得分:2)

请参阅:

https://snowball.digital/Blog/Uploading-Images-in-React-Native

https://github.com/kamilkp/react-native-file-transfer

var photo = {
    uri: uriFromCameraRoll,
    type: 'image/jpeg',
    name: 'photo.jpg',
};

或者你可以做,

var body = new FormData();
body.append('authToken', 'secret');
body.append('photo', photo);
body.append('title', 'A beautiful photo!');

xhr.open('POST', serverURL);
xhr.send(body);

答案 1 :(得分:0)

以上博客网址已更改。请参考

https://snowball.digital/blog/uploading-images-in-react-native