【问题标题】:Resume upload after browser crashed浏览器崩溃后继续上传
【发布时间】:2014-03-11 14:29:19
【问题描述】:

我正在上传文件以上传最大 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(它从块中创建一个文件),我使用了这个 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 说:

这将允许在浏览器重新启动后恢复上传,甚至 跨浏览器(理论上你甚至可以运行相同的文件上传 跨多个选项卡或不同的浏览器)。 POST 数据请求 列出的都需要使用 Resumable.js 来接收数据

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

【问题讨论】:

  • 我自己找到了答案,这确实很明显。如果您使用我采用的相同 PHP 后端代码,它已经实现并且开箱即用。您只需要在浏览器崩溃或其他任何情况后重新上传相同的文件,resumable.js 将自动从您停止的地方恢复上传。抱歉这个愚蠢的问题。
  • 您可以添加您的评论作为答案..以便其他用户将来会发现它有用..

标签: php file-upload resumablejs


【解决方案1】:

你可以使用this package

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

【讨论】:

    猜你喜欢
    • 2020-02-27
    • 1970-01-01
    • 2016-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-10
    • 2013-01-06
    • 1970-01-01
    相关资源
    最近更新 更多