Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 恢复下载_C#_Asp.net Mvc_Http_Model View Controller_Stream - Fatal编程技术网

C# 恢复下载

C# 恢复下载,c#,asp.net-mvc,http,model-view-controller,stream,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

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

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

    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();
            }
        }
    }
当我进入我的页面时,它可以完美地下载,但问题是如果我停止下载,它将不会再次恢复

我搜索了这个问题,发现为了支持resume,必须在连接中定义一个标题“Accept Ranges”

所以我添加了标题,但没有起作用。

处理比这稍微复杂一点。通常,您需要处理请求中的
Range
If Range
标题,并使用
Content Range
Date
ETag
Content Location
标题提供正确的206个部分内容响应

本文详细介绍了如何使用范围请求支持创建ASP.NET MVC
ActionResult


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

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