Background
When I upload a file to my Express Node
server, the file uploads to the correct directory but an error occurs.
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 Error: Request has been terminated
Possible causes: the network is offline, Origin is not allowed by Access-Control-Allow-Origin, the page is being unloaded, etc. at Request.crossDomainError (client.js?186f:621) at XMLHttpRequest.xhr.onreadystatechange (client.js?186f:703)
After uploading the file I am trying to read it and then process it so I can enter it to the mysql
database. The first time I enter file, the error is thrown and the data is not read, nor inserted to the database. Even though it is uploaded to the server successfully.
Once the file exists on the server, if I upload the same file again, it will then read it, insert it to the database and complete without error.
I assumed the problem was the file had not finished uploading, but I do not understand why that would throw the error I am receiving.
What I Have Tried
- I set a timeout to make sure the file had uploaded to the server.
- I installed the
npm cors package
. - I added more callback functions to try and make sure the upload finished.
I commented out all of the code to focus only on getting the file to upload without an error. But it continues to throw the error on the first file upload. Even though it is uploaded successful.
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
});
Question
What is the cause of this error?
* EDIT *
My 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);
}
The Dropzone
<Dropzone
style={dropZoneStyles}
accept = ".pdf,.xlsx,.XLSX,.csv"
onDrop={(accepted, rejected) => { this.onDrop(accepted, rejected); }}>...</Dropzone>