恢复下载

5

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

目前我已经成功实现了这个功能,以下是我的代码:

    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”才能支持恢复。所以我添加了该标题,但没有起作用。
1个回答

15
处理 范围请求 比较复杂。通常需要处理请求中的 RangeIf-Range 标头,并返回适当的 206 Partial Content 响应,包括 Content-RangeDateETagContent-Location 标头。
文章 "Range Requests in ASP.NET MVC – RangeFileResult"详细介绍了如何创建具有范围请求支持的ASP.NET MVC ActionResult
在您的情况下,您还需要检查另一方(您使用fileReq的那一方)是否支持范围请求。如果是,则可以仅请求所需部分(最好将其缓存到本地),但如果不是,则需要获取整个文件并跳转到正确的位置(在这种情况下,您肯定需要有一个本地缓存方案)。

非常感谢,那是一个完美的答案 :) - BOSS
@PurTahan 我已经更新了链接,尽管之前的链接能够正确地重定向到文章。 - tpeczek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接