c# - 恢复下载

标签 c# asp.net-mvc http model-view-controller stream

想法很简单,我正在创建一项服务,用户可以在其中放置托管在另一个网站上的文件的直接链接,我的程序将打开到该远程服务器的流并开始以字节为单位读取文件,然后将每个读取的字节返回给用户。

到目前为止我设法让它工作,这是我的代码

    public void Index()
    {
        //Create a stream for the file
        Stream stream = null;

        //This controls how many bytes to read at a time and send to the client
        int bytesToRead = 10000; //10000

        // Buffer to read bytes in chunk size specified above
        byte[] buffer = new Byte[bytesToRead];

        // The number of bytes read
        try
        {
            //Create a WebRequest to get the file
            HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create("http://SOME-OTHER-SERVER.com/File.rar");


            //Create a response for this request
            HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();


            if (fileReq.ContentLength > 0)
                fileResp.ContentLength = fileReq.ContentLength;

            //Get the Stream returned from the response
            stream = fileResp.GetResponseStream();

            // prepare the response to the client. resp is the client Response
            var resp = HttpContext.Response;

            //Indicate the type of data being sent
            resp.ContentType = "application/octet-stream";

            //Name the file 
            resp.AddHeader("Content-Disposition", "attachment; filename=\"" + "fle.rar" + "\"");
            resp.AddHeader("Content-Length", (fileResp.ContentLength).ToString());

            int length;
            do
            {
                // Verify that the client is connected.
                if (resp.IsClientConnected)
                {
                    // Read data into the buffer.
                    length = stream.Read(buffer, 0, bytesToRead);

                    // and write it out to the response's output stream
                    resp.OutputStream.Write(buffer, 0, length);

                    // Flush the data
                    resp.Flush();

                    //Clear the buffer
                    buffer = new Byte[bytesToRead];
                }
                else
                {
                    // cancel the download if client has disconnected
                    length = -1;
                }
            } while (length > 0); //Repeat until no data is read
        }
        finally
        {
            if (stream != null)
            {
                //Close the input stream
                stream.Close();
            }
        }
    }

当我转到我的页面时,它可以完美下载,但问题是如果我停止下载,它就不会再次恢复。

我搜索了那个问题,发现必须在连接中定义标题“Accept-Ranges”才能支持恢复。

所以我添加了那个标题但没有用。

最佳答案

处理 Range Requests 比这稍微复杂一点。通常,您需要处理请求中的 RangeIf-Range header ,并使用 Content-Range< 提供适当的 206 Partial Content 响应DateETagContent-Location header 。

文章Range Requests in ASP.NET MVC – RangeFileResult详细描述了如何创建具有范围请求支持的 ASP.NET MVC ActionResult

在您的情况下,您还必须检查另一侧(您使用 fileReq 的那一侧)是否支持Range Request。如果是,那么您可以只请求所需的部分(最好将其缓存在本地某处),但如果不是,则需要获取整个文件并寻找适当的位置(在这种情况下,您肯定希望有一个本地缓存方案)。

关于c# - 恢复下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15609558/

相关文章:

javascript - 使用文件类型输入发布 $http 请求并获取 req.body 和 req.file 不使用 multer nodejs

c# - ef6 中的第一次运行问题

javascript - Google 饼图未在浏览器中显示

c# - 作为查询字符串附加的属性

asp.net-mvc - 无法加载指定的元数据资源

python - 强制python http请求刷新

node.js - 如何从 Node.js/Express 中的请求中检索实体主体(用于 CSP 违规报告)?

c# - 为什么在调用非静态方法时会调用结构中的静态构造函数?

c# - 从 SQL Server 中的 SELECT 获取 bool 值到 C# 中的 bool 值?

asp.net-mvc - 在没有 SSL 的情况下保护 API 调用的最佳方式?