Re-uploading a file with AJAX after it was changed causes net::ERR_UPLOAD_FILE_CHANGED in Chrome

后端 未结 6 1696
悲&欢浪女
悲&欢浪女 2021-02-02 18:32

I have a simple HTML form that sends an selected file via AJAX to an API endpoint.

If I do the following steps:

  1. Press the Upload button and make an POST r
6条回答
  •  走了就别回头了
    2021-02-02 18:52

    A quick workaround would be to include a complete parameter to AJAX call (or any equivalent of a finally call that always gets invoked) and add code that resets the form. I've noticed that attaching the file again solves it. While I appreciate it's not a real solution to the problem, it does provide handling or rather avoidance of that error.

    This way, if the user for any reason needs to re-upload the file, they'll have to choose the file again.

    Example below:

    $.ajax({
        url: this.action,
        type: this.method,
        data: this.data,
        success: function (response) { 
            // success scenario
        },
        error: function (result) {
            // error scenario
        },
        complete: function (data) {
            $('#uploadForm')[0].reset(); // this will reset the form fields
        }
    });
    

提交回复
热议问题