Resumable file upload customize name and add additional parametrs

1.7k views Asked by At

I'm using http://resumablejs.com/ and can't understand how I can change filename after upload.

Describe a little more my situation:

I have file UploadFile.php with default code:

include 'vendor/autoload.php';

use Dilab\Network\SimpleRequest;
use Dilab\Network\SimpleResponse;
use Dilab\Resumable;

$request = new SimpleRequest();
$response = new SimpleResponse();

$resumable = new Resumable($request, $response);
$resumable->tempFolder = 'tmps';
$resumable->uploadFolder = 'upload/video';

$resumable->process();

I know that if I will use following:

$originalName = $resumable->getOriginalFilename(Resumable::WITHOUT_EXTENSION);
$slugifiedname = 'custom_prefix_'.$originalName;
$resumable->setFilename($slugifiedname);

It's will add 'custom_prefix_' to my filename. But! I need use for prefix some additional information from form (Firstname and Lastname), how I can add this information to my request?

In frontend my file looks like:

<script type="text/javascript">
        window.onload = (function () {
            var r = new Resumable({
                target: '/UploadFile.php',
                maxChunkRetries: 2,
                maxFiles: 1,
                prioritizeFirstAndLastChunk: true,
                simultaneousUploads: 4,
                chunkSize: 5 * 1024 * 1024,
                uploadMethod: 'POST',
                maxFileSize: 550 * 1024 * 1024
            });

...

    uploadFile.on('click', function () {
        $('.valid').html('');
        if (results.children().length > 0) {
            $.ajax({
                url: '/Validate.php',
                type: "POST",
                data: $('#upload_form').serialize()+'&fileType='+fType+'&fileName='+fName,
                dataType: "json",
                success: function (data) {
                    if (results.children().length > 0) {
                        if(data[0]==true && data[1]==true){
                            $.ajax({
                                url: '/FormUpload.php',
                                type: "POST",
                                data: $('#upload_form').serialize()+'&fileType='+fType+'&fileName='+fName,
                                success: function (data) {
                                    if (results.children().length > 0) {
                                        r.upload();
                                    } else {
                                        nothingToUpload.fadeIn();
                                        setTimeout(function () {
                                            nothingToUpload.fadeOut();
                                        }, 3000);
                                    }
                                }
                            });
                        }else{
                            if(data[0]==false){
                                valid.text('Please complete all required fields!');
                            }
                            if(data[1]==false){
                                valid.text('Please complete all exeption fields!');
                            }
                        }

                    } else {
                        nothingToUpload.fadeIn();
                        setTimeout(function () {
                            nothingToUpload.fadeOut();
                        }, 3000);
                    }
                }
            });
        } else {
            nothingToUpload.css('opacity', 1);
            setTimeout(function () {
                nothingToUpload.css('opacity', 0);
            }, 3000);
        }
    });
2

There are 2 answers

0
amedv On BEST ANSWER

I found solution in my file FormUpload.php I return necessary information and send it like this:

uploadFile.on('click', function () {
    $('.valid').html('');
    if (results.children().length > 0) {
        $.ajax({
            url: '/Validate.php',
            type: "POST",
            data: $('#upload_form').serialize()+'&fileType='+fType+'&fileName='+fName,
            dataType: "json",
            success: function (data) {
                if (results.children().length > 0) {
                    if(data[0]==true && data[1]==true){
                        $.ajax({
                            url: '/FormUpload.php',
                            type: "POST",
                            data: $('#upload_form').serialize()+'&fileType='+fType+'&fileName='+fName,

                            success: function (data) {
                                var data = jQuery.parseJSON(data);
                                if (results.children().length > 0) {
                                   r.files[0].fileName = data.new_file_name;
                                   r.upload();
                                } else {
                                    nothingToUpload.fadeIn();
                                    setTimeout(function () {
                                        nothingToUpload.fadeOut();
                                    }, 3000);
                                }
                            }
                        });
                    }else{
                        if(data[0]==false){
                            valid.text('Please complete all required fields!');
                        }
                        if(data[1]==false){
                            valid.text('Please complete all exeption fields!');
                        }
                    }

                } else {
                    nothingToUpload.fadeIn();
                    setTimeout(function () {
                        nothingToUpload.fadeOut();
                    }, 3000);
                }
            }
        });
    } else {
        nothingToUpload.css('opacity', 1);
        setTimeout(function () {
            nothingToUpload.css('opacity', 0);
        }, 3000);
    }
});
1
Webistan On

Try this code:

$resumable->process();

// you can get file information after the upload is complete
if (true === $resumable->isUploadComplete()) { // true when the final file has been uploaded and chunks reunited.
    $extension = $resumable->getExtension();
    $filename = $resumable->getFilename();
}