在asp.net中下载文件 [英] Download file in asp.net
本文介绍了在asp.net中下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要可以帮助在asp.net中下载文件的代码
如何异步下载文件..
请帮助我成为一名优秀的程序员.
I wants code that helps for downloading files in asp.net
How to download file Asynchrnously..
Please Help me to be a good coder.
推荐答案
此链接将帮助您
在asp.net中异步下载的问题 [ ^ ]
祝您好运
Hi,
this link will help u
problem with asynchronously downloading in asp.net[^]
best luck
public static void download(string fileName, string filepath)
{
string userAgent = HttpContext.Current.Request.Headers.Get("User-Agent");
string f = HttpContext.Current.Server.UrlDecode(fileName);
if (userAgent.Contains("MSIE 7.0"))
{
//for IE
f = fileName.Replace(" ", "%20");
}
else
{
//do nothing
}
fileName = HttpContext.Current.Server.UrlDecode(fileName);
System.IO.FileStream liveStream = new System.IO.FileStream(HttpContext.Current.Server.MapPath(filepath), System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] buffer = new byte[Convert.ToInt32(liveStream.Length)];
liveStream.Read(buffer, 0, Convert.ToInt32(liveStream.Length));
liveStream.Close();
HttpContext.Current.Response.Clear();
if (fileName.EndsWith(".csv"))
{
HttpContext.Current.Response.ContentType = "application/csv";
}
else
{
HttpContext.Current.Response.ContentType = "application/octet-stream";
}
HttpContext.Current.Response.AddHeader("Content-Length", buffer.Length.ToString());
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + f + "\"");
HttpContext.Current.Response.BinaryWrite(buffer);
HttpContext.Current.Response.End();
}
这篇关于在asp.net中下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文