如何以最简单的方式将react-native文件上传到服务器? 在线上有太多的信息和库,因此我不知道它们是否可以安全使用。
谢谢。
答案 0 :(得分:2)
This is the method to upload file to server using fetch
const body = new FormData();
//String Key value pair appending to body
body.append('KEY', VALUE);
body.append('KEY', VALUE);
body.append('KEY', VALUE);
//Appending file to body
body.append(KEY_AS REQUIRED_IN_SERVICE, {
uri: PASS_URI_OF_THE_FILE,
type: 'image/jpeg', //This is the file type .. you can define according to your requirement
name: 'photo.jpg', //File name you want to pass
})
//Service call
fetch(YOUR_URL, {
method: 'POST',
headers: new Headers({
YOUR_HEADER_PARAMS
}),
body: body
})
.then(res => res.json())
.then((responseJson) => {
//GET RESPONSE SUCCESS OF FAILURE
})
.catch((error) => {
//ERROR
});
}
答案 1 :(得分:0)
尝试https://github.com/itinance/react-native-fs
我们在某些应用中将其用于生产中