【问题标题】:Laravel Chunk Upload skipping one chunkLaravel 块上传跳过一个块
【发布时间】:2020-07-13 02:50:44
【问题描述】:

我将laravel-chuck-uploaddropzone.js 结合使用。一切正常,块正在上传,上传时最终文件将保存在 S3 中。问题是总是缺少 1 个块。有时 .8.part 丢失,有时 .7.part 丢失(上传后它们保留在 chunks 目录中)。当我上传总大小为 9.7MB 的视频时,S3 中的文件为 8.7MB。那是缺少 1mb,与缺少的部分大小相同。所有块的大小均为 1MB。

可能是什么问题,我该如何解决?

编辑:我想我找到了问题,但没有找到解决办法。上传最后一个块(第 10 个)时,它认为所有块都已上传,但第 8 个块尚未完成上传。

【问题讨论】:

    标签: php laravel laravel-5 chunked


    【解决方案1】:

    用我自己的 isLastChunk 方法扩展了 DropZoneUploadHandler。这样,当文件真的是最新块时,文件就会被上传。唯一的问题是 dropzone.js 会为最后一个块调用而不是最后一个完成的块触发 chunksUploaded 事件。

    public function isLastChunk()
    {
        $chunkFileName = preg_replace(
            "/\.[\d]+\.".ChunkStorage::CHUNK_EXTENSION.'$/', '', $this->getChunkFileName()
        );
    
        $files = ChunkStorage::storage()->files(function ($file) use ($chunkFileName) {
            return false === Str::contains($file, $chunkFileName);
        });
    
        return (count($files) + 1) == $this->getTotalChunks();
    }
    

    为了解决这个问题,我在 dropzone.js 中添加了一个 chunkUploaded 函数来获取每个块上传的响应。现在我可以像以前一样做所有事情,但文件包含所有块。

    我在 dropzone.js 中添加了以下内容:

    /**
     * The callback that will be invoked when a single chunk has been uploaded for a file.
     * It gets the file for which the chunks have been uploaded as the first parameter,
     * and the `done` function as second. `done()` needs to be invoked when everything
     * needed to finish the upload process is done.
     */
    chunkUploaded: function chunkUploaded(file, response, statuscode, done) {
        done();
    },
    
    xhr.onreadystatechange = function () {
        if(this.readyState == 4) {
            _this16.options.chunkUploaded(file, this.responseText, this.status, function () {
                _this16._finished(files, '', null);
            });
        }
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-10
      • 1970-01-01
      • 2021-05-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 2018-05-07
      相关资源
      最近更新 更多