Search code examples
javascriptjqueryfile-uploadblueimp

blueimp jquery-file-upload how to cancel 1 file before upload


I am using the following library https://github.com/blueimp/jQuery-File-Upload/wiki

I am showing the selected files after (add files) button is selected. I am not showing the demo grid showing cancel button or upload

I have a button which does the submit (see below). I would like to cancel the upload of one of the files selected before this upload button is selected.

Walking through the code i see that when i finally hit submit, it calls fileuploadsubmit for each file uploaded.

I would like to show to the user grid of files selected, then allow them to cancel one of them by picking a link to remove from data. All the samples i am finding online tend to parent().remove() thereby removing from visual grid.

How can i do without showing that grid?

ie..

                ...
                add: function (e, data) {

                $.each(data.files, function (index, file) {
                    files += file.name;
                });
                $('#files').text(files);

                viewModel.Add(data);

                data.context = $('#btnUpload')
                    .click(function () {
                        data.submit();
                        return false;
                    });
            },
            ......


            $('#fileupload').bind('fileuploadsubmit', function (e, data) {
            // The example input, doesn't have to be part of the upload form:
            //var input = $('#input');
            //data.formData = {example: input.val()};

            var countryId = 1; //viewModel.selectedCountry().CountryId()
            var selected = 'test'; //$('#lstTemplate option:selected').text();

            data.formData = { templateType: selected, countryId: countryId };
            if (data.formData.templateType == 'Select') {
                data.context.find('button').prop('disabled', false);
                return false;
            }
        });

Solution

  • Actually is possible to remove one file from data.files. I'm using KnockoutJs web-component in which I'm storing instance of "upload object" and I have observable array for file list ...

    My configuration is:

    .fileupload({
                url: DataServiceBaseUrl + '/api/Ruleset/BatchImport',
                dataType: 'json',
                singleFileUploads: false,
                replaceFileInput: false,
                add: function (e, data) {
    ...
    

    what I did is

    this.RemoveFile = function(index){
       this.UploadInstance.files.splice(index,1);
       this.UploadFiles.splice(index,1);
    }
    

    and template was:

     <!-- ko foreach: UploadFiles -->
        <div><span data-bind="text: name"></span><button data-bind="click: function() { $component.RemoveFile($index()); }">delete</button></div>
     <!-- /ko -->