Загрузка чанка на Laravel 5 & AJAX - PullRequest
0 голосов
/ 02 марта 2019

Я попробовал несколько способов сделать загрузку по частям, но у меня все равно не получилось, и я все еще пытаюсь, но без пользы. Любая помощь, я хочу использовать этот пакет:

pionl / laravel-загрузка чанка

Загрузка файлов через AJAX с помощью jQuery и частичной загрузки, вот что я попробовал:

<script type="text/javascript" src="{{ asset('js/jquery.iframe-transport.js') }}"></script>
<script type="text/javascript" src="{{ asset('js/jquery.fileupload.js') }}"></script>
<script type="text/javascript" src="{{ asset('js/jquery.ui.widget.js') }}"></script>

<div class="text-center">
    <input id="fileupload" type="file" name="file" data-url="{{ url('upload') }}" style="display: inline;">
    <ul id="file-upload-list" class="list-unstyled">
    </ul>
</div>

<script type="text/javascript">
var $ = window.$; // use the global jQuery instance

var $uploadList = $("#file-upload-list");
var $fileUpload = $('#fileupload');
if ($uploadList.length > 0 && $fileUpload.length > 0) {
    var idSequence = 0;

    // A quick way setup - url is taken from the html tag
    $fileUpload.fileupload({
        maxChunkSize: 1000000,
        method: "POST",
        // Not supported
        sequentialUploads: false,
        formData: function (form) {
            // Append token to the request - required for web routes
            return [{name: '_token', value: $('input[name=_token]').val()}];
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $("#" + data.theId).text('Uploading ' + progress + '%');
        },
        add: function (e, data) {
            data._progress.theId = 'id_' + idSequence;
            idSequence++;
            $uploadList.append($('<li id="' + data.theId + '"></li>').text('Uploading'));
            data.submit();
        },
        done: function (e, data) {
            console.log(data, e);
            $uploadList.append($('<li></li>').text('Uploaded: ' + data.result.path + ' ' + data.result.name));
        }
    });
}
</script>

Мой контроллер:

public function upload(Request $request) {
    // create the file receiver
    $receiver = new FileReceiver("file", $request, HandlerFactory::classFromRequest($request));
    // check if the upload is success, throw exception or return response you need
    if ($receiver->isUploaded() === false) {
        throw new UploadMissingFileException();
    }
    // receive the file
    $save = $receiver->receive();
    // check if the upload has finished (in chunk mode it will send smaller files)
    if ($save->isFinished()) {
        // save the file and return any response you need, current example uses `move` function. If you are
        // not using move, you need to manually delete the file by unlink($save->getFile()->getPathname())
        return $this->saveFile($save->getFile());
    }
    // we are in chunk mode, lets send the current progress
    /** @var AbstractHandler $handler */
    $handler = $save->handler();
    return response()->json([
        "done" => $handler->getPercentageDone(),
        'status' => true
    ]);
}

Любой успешныйспособ сделать это?

...