Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/three.js/2.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中发送HTTPResponse#_C#_.net_Httpwebrequest_Httpwebresponse - Fatal编程技术网

C# 在库文件c中发送HTTPResponse#

C# 在库文件c中发送HTTPResponse#,c#,.net,httpwebrequest,httpwebresponse,C#,.net,Httpwebrequest,Httpwebresponse,我用下面的代码下载了asp.net网站中的文件夹文件 string path = @"E:\sample.zip"; FileInfo file = new FileInfo(path); int len = (int)file.Length, bytes; Response.ContentType = "text/html"; // Response.AddHeader "Content-Disposit

我用下面的代码下载了asp.net网站中的文件夹文件

 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;
    }
}
它很好用

但我的问题是,当我对库文件使用与

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;
                }
            }
        }
这给我带来了一个错误

使用自定义TextWriter时,OutputStream不可用。

我想问题就在那一行

 HttpResponse Response = new HttpResponse(TextWriter.Null);
你能给我一个解决方案吗


正在等待您的响应……

您可以尝试使用此代码

TextWriter sw = new StringWriter();
HttpResponse Response = new HttpResponse(sw);

我将结构替换为

   HttpResponse response = HttpContext.Current.Response;
它很好用

谢谢大家的支持