How to upload a file only once with blueimp file upload plugin?
Asked Answered
M

3

12

I'm using the bluimp jQuery-File-Upload-plugin . It's no problem to select some files and upload them, but when I want to upload another files without refreshing the page, the first ones are getting uploaded again. My question is how can I "unset" files after they are uploaded. Here is my sourcecode

Javascript:

$('#MappeFile').fileupload({
        dataType : 'json',
        autoUpload : false,
        maxNumberOfFiles : undefined,
        maxFileSize : 6000000,
        minFileSize : undefined,
        acceptFileTypes : /.+$/i,
        url : "/ajax/UploadFile.php",
        add : function(e, data) {
            $("#testUploadButton").on("click", function() {
                    $('#progress .bar').show();
                    if ($.browser.msie && parseInt($.browser.version, 10) < 10) {
                        $('#progress .bar').css({
                            "background" : "url(images/progressbar.gif) no-repeat",
                            "width" : "100%"
                        })
                    } else {
                        $('#progress .bar').css({
                            'background-color' : "#2694E8",
                            'width' : '0%'
                        });
                    }
                data.submit();
            })
        },
        change : function(e, data) {
            $.each(data.files, function(index, file) {
                console.info('Selected file: ' + file.name);
                filesCount++;
            });
        },
        drop: function(e, data) {
            $.each(data.files, function(index, file) {
                console.info('Selected file: ' + file.name);
                filesCount++;
            });
        },
        done : function(e, data) {
            $.each(data.result, function(index, file) {
                vOutput = "<tr>";
                vOutput += "<td>" + file + "</td>";
                vOutput += "<tr>";
                $("#MappeFileListe").append(vOutput);
                filesUploaded++;
                if (filesCount == filesUploaded) {
                    filesUploaded = 0;
                    filesCount=0;
                    $('#progress .bar').hide();
                }
            });
        },
        progressall : function(e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('#progress .bar').css('width', progress + '%');
        }
    });

HTML:

<div id="KundeMappe">
    <form id="MappeFile">
        <input type="file" id="MappeFileSelect" name="files[]" data-url="ajax/UploadFile.php" multiple/>
        <div id="progress">
            <div class="bar" style="width: 0%;"></div>
        </div>
        <input type="button" class="neuButton" value="upload" id="testUploadButton"/>
    </form>
    <table id="MappeFileListe"></table>
</div>
Mate answered 9/11, 2012 at 8:55 Comment(0)
M
22

I found the answer myself - It's enough to unbind the click event of the button after upload:

add : function(e, data) {
            $("#testUploadButton").on("click", function() {
                    $('#progress .bar').show();
                    if ($.browser.msie && parseInt($.browser.version, 10) < 10) {
                        $('#progress .bar').css({
                            "background" : "url(images/progressbar.gif) no-repeat",
                            "width" : "100%"
                        })
                    } else {
                        $('#progress .bar').css({
                            'background-color' : "#2694E8",
                            'width' : '0%'
                        });
                    }
                data.submit();
                $("#testUploadButton").off("click")
            })
        },
Mate answered 9/11, 2012 at 13:31 Comment(5)
I have the same problem. But with your solution it just suppress all the other requests, still allocating memory. How to restrict the number of files, to avoid adding the selected files to the upload queue? Setting maxNumberOfFiles: 1 doesn't work for me.Murry
is it better to set the $("#testUploadButton").off("click") before the new $("#testUploadButton").on("click", function() { ... } ?Murry
Your solution does not seem to be the RIGHT answer, but it did the trick for me thanks alot.Cadaverine
The reason why there are multiple file uploads happening is because each time fileupload() is called a new click event is being added to the testUploadButton. As this answer shows, removing the click event from the button resolves this problem. There is a jquery function one() which will bind the click event to the button and then unbind the click after after the first invocation. api.jquery.com/one This would result is the following change: $("#testUploadButton").one("click", function() {Emmettemmey
Great solution. I put it before my click handler so that only the most recently selected file is uploaded.Shod
B
2

I had a similar problem where previously uploaded files were included in the next upload. You can try following solution below:

On Add Function just add "change" event of the file input element like below:

$('#YourFileUploadElementId').change(function(e) {
     data.files.splice(0); // Clear All Existing Files
});

Full Example Below:

$('#YourFileUploadElementId').fileupload({
    // Some options
    add: function (e, data) {
        $('#YourFileUploadElementId').change(function(e) {
          data.files.splice(0); // Clear All Existing Files
        });
    },
    // Other Events
 });

Note: Just change the YourFileUploadElementId to your file upload element id.

Here is the complete example on jsfiddle.net

http://jsfiddle.net/dustapplication/cjodz2ma/5/

Borak answered 25/3, 2020 at 11:42 Comment(0)
S
1

The same problem when I faced, I resolved it by using one() in place of on(). The add function can be written as:

add : function(e, data) {
        $("#testUploadButton").one("click", function() {
                $('#progress .bar').show();
                if ($.browser.msie && parseInt($.browser.version, 10) < 10) {
                    $('#progress .bar').css({
                        "background" : "url(images/progressbar.gif) no-repeat",
                        "width" : "100%"
                    })
                } else {
                    $('#progress .bar').css({
                        'background-color' : "#2694E8",
                        'width' : '0%'
                    });
                }
            data.submit();
        })
    }

The .one() method is identical to .on(), except that the handler for a given element and event type is unbound after its first invocation.

Here is more about one(): https://api.jquery.com/one/

Sartor answered 14/9, 2022 at 7:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.