【发布时间】:2019-07-05 16:24:26
【问题描述】:
我正在尝试使用react-native-document-picker 将文件上传到服务器。我面临的问题是我不知道如何将文件与文本一起上传。在我的应用程序中有一部分用于文件上传,也有一个区域用于编写一些文本。然后它将被上传到服务器.所以我做了以下。但是提交到服务器后我收到了这个错误
unhandled promise rejection unsupported BodyInit type
更新部分代码
filepick = () => {
DocumentPicker.show({
filetype: [DocumentPickerUtil.images()],
}, (error, res) => {
if (error == null) {
console.log(
res.uri,
res.type, // mime type
res.fileName,
res.fileSize
);
this.setState({
img_uri: res.uri,
img_type: res.type,
img_name: res.fileName
})
} else {
Alert.alert('Message', 'File uploaded failed');
}
});
};
onPressSubmit() {
const data = new FormData();
data.append('file', { uri: this.state.img_uri, type:
this.state.img_type, name: this.state.img_name })
data.append('comment', { text: this.state.text });
AsyncStorage.getItem("userdetail").then(value => {
fetch(GLOBAL.ASSN_URL +`${this.props.id}`, {
method: 'POST',
headers: {
'Accept': 'application/json',
'Content-Type': 'multipart/form-data',
'Authorization': value
},
body: data
}).then((response) => {
return response.text()
}).then((responseJson) => {
var result = responseJson;
console.log(result);
});
})
}
函数filepick()在从您的设备中选择一个文件后被调用。请帮我找到一个解决方案。我如何将它上传到服务器以及如何发送文本而不对其进行字符串化?
【问题讨论】:
标签: javascript reactjs react-native file-upload multipartform-data