Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Blueimp multiple chunk upload with form data

I want to chunk upload multiple files with form data. Save form data to database and image to a perticualr folder.

I'm using blueimp upload here is my Fiddle.

The JavaScript codde i'm using

$(function () {
    $('#fileupload').fileupload({
        maxChunkSize: 5000,
        previewMaxHeight: 210,
        previewMaxWidth: 210,
        url: '/echo/json'
    });
    $('#fileupload').bind('fileuploadsubmit', function (e, data) {
        var inputs = data.context.find(':input');
        if (inputs.filter('[required][value=""]').first().focus().length) {
            return false;
        }
        data.formData = inputs.serializeArray();
    });
});

Chunk upload is working fine, but if I save data to database multiple entries are created. The number of entries created in database is equal to number chunk uploaded.

enter image description here

The PHP code that i'm using is ( taking help of blueimp PHP class https://github.com/blueimp/jQuery-File-Upload/blob/master/server/php/UploadHandler.php )

public function postUpload()
{
    $upload_handler = new UploadHandler(); // Blueimp class

    $this->file->create(Input::all()); // This code is executed multiple times

}

So main trouble is $this->file->create(Input::all()); code is execute multiple times as the number of chunks uploaded, where as I want it to be executed once when file is uploaded successfully.

Also want to name file of the file that is uploaded to save it to database.

like image 460
Basic Bridge Avatar asked Jul 02 '26 12:07

Basic Bridge


1 Answers

You need to first upload all the chunks and on the last chunk execute your own logic. So here is how you can do this.

You should first extract the range info from http header Content-Range. You can look at this answer for how to. Then you should check if that's the last chunk.

And if it is the last chunk you simly execute your business logic there.

Even if it is a late answer, it might help someone.

like image 55
Tolga Evcimen Avatar answered Jul 05 '26 03:07

Tolga Evcimen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!