用我自己的 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);
});
}
};