Search code examples
javascriptfine-uploader

How can i remove some file from fineuploader before upload


I'm using Fine Uploader with multiple file upload, which works great. Now I ran some MD5 checks in the submit event, so I can determine if the file is already uploaded, but I don't know how to remove this files from the upload query or storedFiles before manually triggered upload. this is my code:

<script type="text/javascript">
    $("#uploader").fineUploader({
        request: {
            endpoint: 'home/upload'
        },
        multiple: true, 
        autoUpload: false,                                      
        editFilename: {                                            
            enable: true
        }
    }).on({                                                      
        "complete": function (event, id, fileName, responseJSON) { 
            if (responseJSON.success) {
                alert("upload success");
            }
        },

        "submit": function (event,id, fileName) {             
            var f=$(this).fineUploader('getFile',id);
            var xxxx = CheckMD5(f, id);
            if (xxxx==false) {               //i want ignore the file if checkMD5 return false
                $(this).fineUploader.cancle(id) //this is not working

            }


        },

        "error": function (event, id, fileName, reason) {      
            //alert(reason);
        }
    });


    $("#uploadButton").click(function () {                         
        $('#uploader').fineUploader('uploadStoredFiles');
    });
    function CheckMD5(f,id) { //i'm using spark-md5.js get to get md5 value and compare in server side
        return false; //this is for test
    }
</script>

Solution

  • To remove a file (before it has started or completed uploading), simply cancel it via the cancel API method.

    For example:

    callbacks: {
        onSubmit: function(id) {
            if (fileIsInvalid) {
                this.cancel(id);
            }
        }
    }
    

    A better way to do this, in my opinion, is to test files in an onValidate callback, and simply return false (or return a promise and reject/resolve it when your async validation operation is done) to omit the file.