发送HTT presponses库文件c#文件、HTT、presponses

2023-09-06 16:59:47 作者:从小缺根筋

我用下面的code下载在asp.net网站上的文件夹是

i used the following code to download the file folder in asp.net website are

 string path = @"E:\sample.zip";
        FileInfo file = new FileInfo(path);
           int len = (int)file.Length, bytes;
             Response.ContentType = "text/html";  
          // Response.AddHeader "Content-Disposition", "attachment;filename=" + filename; 
             Response.AppendHeader("content-length", len.ToString());
           byte[] buffer = new byte[1024];

        using(Stream stream = File.OpenRead(path)) {
    while (len > 0 && (bytes =
        stream.Read(buffer, 0, buffer.Length)) > 0)
    {
        Response.OutputStream.Write(buffer, 0, bytes);
        len -= bytes;
    }
}

它工作正常/...

it works fine/...

但我的问题是,当我用同样的code的库文件

but my problem is when i used the same code to the library file as

FileInfo file = new FileInfo(ZipPath);
            int len = (int)file.Length, bytes;
            HttpResponse Response = new HttpResponse(TextWriter.Null);
            Response.ContentType = "text/html";
            Response.AppendHeader("content-length", len.ToString());
            byte[] buffer = new byte[1024];

            using (Stream stream = File.OpenRead(ZipPath))
            {
                while (len > 0 && (bytes =
                    stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    Response.OutputStream.Write(buffer, 0, bytes);
                    len -= bytes;
                }
            }
        }

这引发了我一个错误,因为

it throws me a error as

的OutputStream不可用时一个自定义的TextWriter被使用。

我想这个问题是在该行

 HttpResponse Response = new HttpResponse(TextWriter.Null);

你能提供给我一个解决方案

can you provide me a solution

在等待您的答复....

waiting for your responses....

推荐答案

我更换了结构为

   HttpResponse response = HttpContext.Current.Response;

它工作得很好......

it works fine.....

感谢大家的支持。