可恢复文件上载自定义名称并添加其他参数

时间:2016-12-22 22:39:04

标签: javascript php resumablejs

我使用http://resumablejs.com/并且无法理解上传后如何更改文件名。

再详细描述一下我的情况:

我有使用默认代码的文件UploadFile.php:

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();

我知道如果我将使用以下内容:

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

它会添加' custom_prefix _'到我的文件名。 但!我需要使用来自表单(名字和姓氏)的一些附加信息的前缀,我如何将这些信息添加到我的请求中?

在前端,我的文件看起来像:

<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 个答案:

答案 0 :(得分:0)

试试这段代码:

$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();
}

答案 1 :(得分:0)

我在我的文件FormUpload.php中找到了解决方案我返回必要的信息并发送如下:

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);
    }
});