C# 在库文件c中发送HTTPResponse#
我用下面的代码下载了asp.net网站中的文件夹文件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
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;
它很好用
谢谢大家的支持