gpt4 book ai didi

php - 浏览器崩溃后恢复上传

转载 作者:行者123 更新时间:2023-12-02 04:27:05 29 4
gpt4 key购买 nike

我正在处理文件上传,以上传最大 2GB 的大文件。因此,我需要确保即使在浏览器崩溃或其他情况后也能恢复下载。 resumable.js对我来说看起来很有希望,所以我尝试了一下:

<a href="#" id="browseButton">Select files</a>
<!-- JAVASCRIPT -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="js/resumable.js"></script>
<script>
var r = new Resumable({
target: "upload.php"
});

r.assignBrowse(document.getElementById("browseButton"));

r.on('fileAdded', function(data) {
// File added, start uploading
r.upload();
});

r.on('fileProgress', function(data) {
console.log(Math.floor(r.progress()*100) + '%');
});
</script>

对于我的 upload.php (它从 block 中创建一个文件),我使用了这个 php 后端示例:https://github.com/23/resumable.js/blob/master/samples/Backend%20on%20PHP.md

上传工作正常,即使是大文件,但如果我不小心关闭了打开的选项卡或浏览器,我找不到任何有用的内容来恢复下载。 php 后端脚本似乎已经实现了一些东西来完成这项工作:

//check if request is GET and the requested chunk exists or not. this makes testChunks work
if ($_SERVER['REQUEST_METHOD'] === 'GET') {

$temp_dir = 'temp/'.$_GET['resumableIdentifier'];
$chunk_file = $temp_dir.'/'.$_GET['resumableFilename'].'.part'.$_GET['resumableChunkNumber'];
if (file_exists($chunk_file)) {
header("HTTP/1.0 200 Ok");
} else
{
header("HTTP/1.0 404 Not Found");
}
}

还有Resumable.js documentation说:

This will allow uploads to be resumed after browser restarts and even across browsers (in theory you could even run the same file upload across multiple tabs or different browsers). The POST data requests listed are required to use Resumable.js to receive data

但是由于我不太擅长服务器端编程/配置,所以我不确定如何实现该功能/检查是否有可以恢复的上传。有没有人遇到类似的问题并可以解释我如何在浏览器重新启动后恢复下载?

最佳答案

您可以使用this package

examples/js-examples/resumable-chunk-upload示例中,您可以关闭并重新打开浏览器,然后恢复未完成的上传。

关于php - 浏览器崩溃后恢复上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21741912/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com