从库文件C#发送HTTP响应 [英] sending httpresponses from library file c#
本文介绍了从库文件C#发送HTTP响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨
我使用以下代码在asp.net网站上下载了文件夹
Hi
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/...
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;
}
}
}
它向我抛出一个错误,因为
使用自定义TextWriter时,OutputStream不可用.
我想问题出在那一行
HttpResponse响应=新的HttpResponse(TextWriter.Null);
能为我提供解决方案吗?
等待您的答复....
it throws me a error as
OutputStream is not available when a custom TextWriter is used.
i guess the problem is in that line
HttpResponse Response = new HttpResponse(TextWriter.Null);
can you provide me a solution
waiting for your responses....
推荐答案
^ ]
http://www.c-sharpcorner. com/Blogs/9383/file-download-in-Asp-Net-with-C-Sharp.aspx [
http://csharpdotnetfreak.blogspot.com/2012/01/download-file-from-server-in-aspnet.html[^]
http://www.c-sharpcorner.com/Blogs/9383/file-download-in-Asp-Net-with-C-Sharp.aspx[^]
这篇关于从库文件C#发送HTTP响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文