我使用以下代码下载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);
你能给我一个解决方案吗?
等待你的回复......
答案 0 :(得分:1)
您可以尝试使用此代码
TextWriter sw = new StringWriter();
HttpResponse Response = new HttpResponse(sw);
答案 1 :(得分:1)
我将结构替换为
HttpResponse response = HttpContext.Current.Response;
它工作得很好.....
感谢大家的支持