I am facing an issue with a basic HTML form that utilizes AJAX to send a selected file to an API endpoint. The problem arises when I follow these steps:
- Click on the Upload button and initiate a POST request to upload the file
- Modify the selected file using a text editor without re-selecting it in the file picker
- Click on the Upload button again, triggering a second POST request
Chrome throws an error for the second request: net::ERR_UPLOAD_FILE_CHANGED
. Interestingly, if you make changes to the file before the initial upload, it goes through without any issues. This error only occurs during the second upload after changing the file following a successful initial upload. I have tested this scenario with CSV files by editing them in a text editor.
Unfortunately, there doesn't seem to be a straightforward solution to isolate and resolve this error.
Is there a workaround for this particular problem?
If not, is there a way to catch this specific error and provide a meaningful message to the user? The promise returned by Fetch does not contain detailed information about this error. The only mention of ERR_UPLOAD_FILE_CHANGED
is in the browser's developer console.
I am quite certain that around a year ago (early 2019), there weren't any issues with this process as users had the ability to re-upload modified files seamlessly within our UI flow. Now we are forced to prompt the user to pick the file again. Hence, I suspect that this error might have been introduced with a recent Chrome update.
Below is a simplified snippet of the code being used:
<html>
<head>
<script type='text/javascript'>
document.addEventListener("DOMContentLoaded", function(){
const button = document.querySelector('#btnSubmit');
button.addEventListener('click', () => {
const form = new FormData(document.querySelector('#theForm'));
const url = '/my-api'
const request = new Request(url, {
method: 'POST',
body: form
});
fetch(request).then(function() {
console.log("ok");
}).catch(function(err) {
console.log(err);
});
});
});
</script>
</head>
<body>
<form enctype="multipart/form-data" action="" method="post" id='theForm'>
<input type="file" name="csvfile" id="csvfile" value="" /></td>
<input type="button" name="uploadCSV" value="Upload" id='btnSubmit'/>
</form>
</body>
</html>
EDIT: This bug has been labeled as WontFix on bugs.chromium.org: https://bugs.chromium.org/p/chromium/issues/detail?id=1086707#c7