背景
当我将文件上传到Express Node
服务器时,文件会上传到正确的目录,但会发生错误。
client.js?186f:772 POST http://localhost:3000/api/v1/odx/debt-uploadnet::ERR_EMPTY_RESPONSE
Request._end @ client.js?186f:772
Request.end @ client.js?186f:676
onDrop @ FileForm.js?9f76:70
onDrop @ FileForm.js?9f76:103
onDrop @ index.js?00e3:236
callCallback @ react-dom.development.js?cada:542
invokeGuardedCallbackDev @ react-dom.development.js?cada:581
invokeGuardedCallback @ react-dom.development.js?cada:438
invokeGuardedCallbackAndCatchFirstError @ react-dom.development.js?cada:452
executeDispatch @ react-dom.development.js?cada:836
executeDispatchesInOrder @ react-dom.development.js?cada:858
executeDispatchesAndRelease @ react-dom.development.js?cada:956
executeDispatchesAndReleaseTopLevel @ react-dom.development.js?cada:967
forEachAccumulated @ react-dom.development.js?cada:935
processEventQueue @ react-dom.development.js?cada:1112
runEventQueueInBatch @ react-dom.development.js?cada:3607
handleTopLevel @ react-dom.development.js?cada:3616
handleTopLevelImpl @ react-dom.development.js?cada:3347
batchedUpdates @ react-dom.development.js?cada:11082
batchedUpdates @ react-dom.development.js?cada:2330
dispatchEvent @ react-dom.development.js?cada:3421
FileForm.js?9f76:71错误:请求已终止
可能的原因:网络处于脱机状态,不允许使用Origin Access-Control-Allow-Origin,页面正在卸载等。 在Request.crossDomainError(client.js?186f:621) 在XMLHttpRequest.xhr.onreadystatechange(client.js?186f:703)
上传文件后,我试图读取它然后处理它,以便我可以将其输入mysql
数据库。第一次输入文件时,抛出错误并且不读取数据,也不插入数据库。即使它已成功上传到服务器。
一旦文件存在于服务器上,如果我再次上传相同的文件,它将读取它,将其插入数据库并完成而不会出错。
我认为问题是文件没有完成上传,但我不明白为什么会抛出我收到的错误。
我尝试过什么
npm cors package
。我注释掉了所有代码,只关注让文件上传而不会出错。但它继续在第一个文件上传时抛出错误。即使它上传成功。
let Storage = multer.diskStorage({
destination: (req, file, callback) => {
callback(null, './uploads');
},
filename: (req, file, callback) => {
callback(null, file.originalname);
}
});
const upload = multer({
storage: Storage
});
router.post('/debt-upload', upload.array('files', 10), (req, res, next) => {
// At this point the file should be uploaded.
// Once the middleware is completed but
// an error is thrown the first time.
// Even though the file is uploaded successful
});
问题
此错误的原因是什么?
*编辑*
我的onDrop()
onDrop(acceptedFiles, rejectedFiles) {
const req = request.post(burl + 'odx/debt-upload');
_.forEach(acceptedFiles, (file) => {
req.attach('files', file);
console.log(file.name);
});
req.end((err, res) => {
if (err) {console.log(err);}
console.log(res);
});
this.showFiles(acceptedFiles);
}
Dropzone
<Dropzone
style={dropZoneStyles}
accept = ".pdf,.xlsx,.XLSX,.csv"
onDrop={(accepted, rejected) => { this.onDrop(accepted, rejected); }}>...</Dropzone>