在C#中以小块下载大文件

时间:2012-11-26 13:54:38

标签: asp.net c#-4.0 httpwebrequest websocket

我需要下载一些超过25 MB的文件,但我的网络只允许请求25 MB的文件。

我正在使用以下代码

  const long DefaultSize = 26214400;
    long Chunk = 26214400;
    long offset = 0;
    byte[] bytesInStream;
    public void Download(string url, string filename)
    {
        long size = Size(url);
        int blocksize = Convert.ToInt32(size / DefaultSize);
        int remainder = Convert.ToInt32(size % DefaultSize);
        if (remainder > 0) { blocksize++; }

        FileStream fileStream = File.Create(@"D:\Download TEST\" + filename);
        for (int i = 0; i < blocksize; i++)
        {
            if (i == blocksize - 1)
            {
                Chunk = remainder;

            }

            HttpWebRequest req = (HttpWebRequest)System.Net.WebRequest.Create(url);
            req.Method = "GET";
            req.AddRange(Convert.ToInt32(offset), Convert.ToInt32(Chunk+offset));
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
            // StreamReader sr = new StreamReader(resp.GetResponseStream());


            using (Stream responseStream = resp.GetResponseStream())
            {
                bytesInStream = new byte[Chunk];
                responseStream.Read(bytesInStream, 0, (int)bytesInStream.Length);
                // Use FileStream object to write to the specified file
                fileStream.Seek((int)offset, SeekOrigin.Begin);
                fileStream.Write(bytesInStream,0, bytesInStream.Length);
            }
            offset += Chunk;

        }
        fileStream.Close();

    }
    public long Size(string url)
    {
        System.Net.WebRequest req = System.Net.HttpWebRequest.Create(url);
        req.Method = "HEAD";
        System.Net.WebResponse resp = req.GetResponse();
        resp.Close();
        return resp.ContentLength;

    }

正确地在磁盘上写内容但内容无法正常工作

4 个答案:

答案 0 :(得分:4)

你应该检查写入之前读了多少,这样(你不需要记住寻找的偏移量,你写的时候自动寻找):

int read;
do
{
    read = responseStream.Read(bytesInStream, 0, (int)bytesInStream.Length);
    if (read > 0)
        fileStream.Write(bytesInStream, 0, read);
}
while(read > 0);

答案 1 :(得分:1)

答案 2 :(得分:0)

范围基于零,您应该从上限减去1.

request.Headers.Range = new System.Net.Http.Headers.RangeHeaderValue(offset, chunkSize + offset - 1);

我在以下链接发布了正确的代码片段: https://stackoverflow.com/a/48019611/1099716

答案 3 :(得分:0)

Akka流可以使用多线程帮助从System.IO.Stream少量下载文件。 https://getakka.net/articles/intro/what-is-akka.html

Download方法会将字节添加到以long fileStart开头的文件中。如果文件不存在,则fileStart值必须为0。

using Akka.Actor;
using Akka.IO;
using Akka.Streams;
using Akka.Streams.Dsl;
using Akka.Streams.IO;

private static Sink<ByteString, Task<IOResult>> FileSink(string filename)
{
    return Flow.Create<ByteString>()
        .ToMaterialized(FileIO.ToFile(new FileInfo(filename), FileMode.Append), Keep.Right);
}

private async Task Download(string path, Uri uri, long fileStart)
{
    using (var system = ActorSystem.Create("system"))
    using (var materializer = system.Materializer())
    {
       HttpWebRequest request = WebRequest.Create(uri) as HttpWebRequest;
       request.AddRange(fileStart);

       using (WebResponse response = request.GetResponse())
       {
           Stream stream = response.GetResponseStream();

           await StreamConverters.FromInputStream(() => stream, chunkSize: 1024)
               .RunWith(FileSink(path), materializer);
       }
    }
}