是否有可能下载的文件(例如,第一个100KB)在C#中的一个部分? [英] Is it possible to download only one part of a file (e.g, the first 100KB) in C#?

查看:119
本文介绍了是否有可能下载的文件(例如,第一个100KB)在C#中的一个部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是好奇,这是否是可能的 - 我知道如何下载一个文件,但我怎么可以下载一个文件只有第一个100KB

I'm just curious about whether this is possible - I know how to download a file, but how can I download only the first 100KB of a file?

推荐答案

试试这个:

        string GetWebPageContent(string url)
        {
            string result = string.Empty;
            HttpWebRequest request;
            const int bytesToGet = 1000;
            request = WebRequest.Create(url) as HttpWebRequest;

//get first 1000 bytes
            request.AddRange(0, bytesToGet - 1);

// the following code is alternative, you may implement the function after your needs
            using (WebResponse response = request.GetResponse())
            {
                using (StreamReader sr = new StreamReader(response.GetResponseStream()))
                {
                    result = sr.ReadToEnd();
                }
            }
            return result;
        }

从<被盗href="http://social.msdn.microsoft.com/Forums/en-US/Vsex$p$pssvcs/thread/ce0e2e13-ec2c-468f-beea-469acb2468fd"相对=nofollow>这里。

这篇关于是否有可能下载的文件(例如,第一个100KB)在C#中的一个部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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