我正在尝试使用multer将一些文件从我的react native客户端上传到nodejs服务器。 如果我尝试使用Postman上传这些文件,那么效果很好,但是当我使用移动应用执行此操作时,会出现以下错误:
Error: Unexpected end of multipart data
at /home/ema/iDiary/iDiary-server/node_modules/dicer/lib/Dicer.js:62:28
at processTicksAndRejections (internal/process/next_tick.js:74:9)
我认为这不是服务器问题,但这是将文件放入以下位置的代码的一部分:
const multer = require('multer');
const multerupload = multer({ dest: 'tmp/' })
v0.post('/upload-attachments', multerupload.any(), uploadAttachments);
这是我的react本机代码,我使用react-native-document-picker获取文件:
DocumentPicker.pick({type: [DocumentPicker.types.allFiles]}).then((res) => {
let split = res.uri.split('/');
let name = split.pop();
let inbox = split.pop();
const realPath = `${RNFS.TemporaryDirectoryPath}${inbox}/${name}`;
this.props.writeTaskData('attachments', {
filepath: realPath,
filename: res.name,
filetype: res.type,
name: res.name
});
console.log('docres',res)
this.props.navigation.navigate('Page5');
})
最后,这是我将文件发送到服务器的代码:
RNFS.uploadFiles({
toUrl: 'https://'+address+':'+port+'/api/v0/upload-attachments',
files: [attachment],
method: 'POST',
headers: {
'Accept': 'application/json',
},
fields: {
homeworkId: res.id,
name: attachment.filename
},
begin: uploadBegin,
progress: uploadProgress
})
如果有人知道解决方案,我将很高兴与他分享解决方案。