如何在 Silex 中实现可恢复下载 [英] How to Implement Resumable Download in Silex

查看:24
本文介绍了如何在 Silex 中实现可恢复下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

silex 中,我可以这样做以强制-下载文件:

In silex I can do this to force-download a file:

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;

$app = new Silex\Application();

// Url can be http://pathtomysilexapp.com/download
$app->get('/download', function (Request $request) use ($app) {
    $file = '/path/to/download.zip';

    if( !file_exists($file) ){
        return new Response('File not found.', 404);
    }

    return $app->sendFile($file)->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, 'download.zip');
});

$app->run();

这适用于较小的文件.但是,我的用例需要下载一个可以由下载管理器暂停/恢复的大文件.

This works well for smaller files. However my use case requires downloading a big that can be paused/resumed by a download manager.

有一个关于文件 streaming 的示例,但似乎没有成为我正在寻找的东西.以前有人这样做过吗?我可以只使用这里的答案并完成它.但如果有一种沉默的方式来做到这一点就好了.

There is an example about file streaming but it doesn't seem to be what I am looking for. Has somebody done this before? I could just use the answer from here and be done with it. But it would be nice if there is a silexy way of doing this.

推荐答案

如果您在驱动器上有文件,那么实现它是非常简单的.这就像对表格中的记录进行分页.

Implementing it is quite trivial if you have a the file on the drive. It is like paginating records from a table.

读取标题,打开文件,查找到所需位置并读取请求的大小.

Read the headers, open the file, seek to the desired position and read the the size requested.

您只需要了解一点关于请求的标头 &回复.

You only need to know a little bit about the headers from the request & response.

服务器是否接受范围:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Range_requests

内容范围的 HTTP 206 状态:

HTTP 206 status for content ranges:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/206

有关内容范围标题的信息:

Info about the content range headers:

https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range

这篇关于如何在 Silex 中实现可恢复下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆