C# FileWebRequest未返回它应该返回的内容

C# FileWebRequest未返回它应该返回的内容,c#,.net,C#,.net,我正在尝试将一个文件从我的计算机发送到浏览器,以便在我正在使用的.NET应用程序中下载。我使用的是SO答案中的代码,但不是使用HttpWebRequest,而是使用FileWebRequest,因为我在本地计算机上访问该文件。请求如下所示: FileWebRequest fileReq=FileWebRequestWebRequest。Create@file://C:/Tmp/new.html;当我复制url时file:///C:/Tmp/new.html 进入浏览器,它会给我正确的文件。但是当

我正在尝试将一个文件从我的计算机发送到浏览器,以便在我正在使用的.NET应用程序中下载。我使用的是SO答案中的代码,但不是使用HttpWebRequest,而是使用FileWebRequest,因为我在本地计算机上访问该文件。请求如下所示: FileWebRequest fileReq=FileWebRequestWebRequest。Create@file://C:/Tmp/new.html;当我复制url时file:///C:/Tmp/new.html 进入浏览器,它会给我正确的文件。但是当我在代码中使用fileReq.ContentLength时,它总是返回0,这使我相信由于某种原因没有读取该文件。谁能告诉我这里发生了什么事

编辑:这是我的代码,就像我在另一个SO问题中说的一样,但我使用了FileWebRequest而不是HttpWebRequest

        Stream stream = null;
        int bytesToRead = 10000;
        byte[] buffer = new Byte[bytesToRead];
        try
        {               
            FileWebRequest fileReq = (FileWebRequest)WebRequest.Create(@"file:///C:/Tmp/new.html");
            FileWebResponse fileResp = (FileWebResponse)fileReq.GetResponse();

            if (fileReq.ContentLength > 0)
            {
                fileResp.ContentLength = fileReq.ContentLength;
                stream = fileResp.GetResponseStream();
                var resp = HttpContext.Current.Response;
                resp.ContentType = "application/octet-stream";
                resp.AddHeader("Content-dsiposition", "attachment; filename=" + url);
                resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());

                int length;
                do
                {
                    if (resp.IsClientConnected)
                    {
                        length = stream.Read(buffer, 0, bytesToRead);
                        resp.OutputStream.Write(buffer, 0, length);
                        resp.Flush();
                        buffer = new Byte[bytesToRead];
                    }
                    else
                    {
                        length = -1;
                    }
                } while (length > 0);
            }
        }
        catch (Exception ex)
        {
            FileLabel.Text = ex.Message.ToString();
        }
        finally
        {
            if (stream != null)
            {
                stream.Close();
            }
        }
尝试WebClient类

 public static void Main (string[] args)
{
    if (args == null || args.Length == 0)
    {
        throw new ApplicationException ("Specify the URI of the resource to retrieve.");
    }
    WebClient client = new WebClient ();

    // Add a user agent header in case the 
    // requested URI contains a query.

    client.Headers.Add ("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705;)");

    Stream data = client.OpenRead (args[0]);
    StreamReader reader = new StreamReader (data);
    string s = reader.ReadToEnd ();
    Console.WriteLine (s);
    data.Close ();
    reader.Close ();
从这里开始:
此方法只接受文件路径,其余部分由您完成。

提供您编写的实际代码。为什么不打开文件并将其输入响应?为什么要用请求/响应对打开它?你考虑过Response.TransmitFile吗?@Alexander这听起来像是我想做的,你能详细说明一下如何做吗?我如何获得HttpResponse对象?就像你在自己的代码中做的那样。或者只使用页面的Response.ok,这会将文件的内容放在实际页面上。我希望用户能够下载实际的文件,但我觉得我已经接近了。