此方法能够实现,在网络环境下的asp.net编程实现的web应用。这里主要也是流式的实现,能够轻松应对各种网络环境的下载实现。
主要用到两个参数:(1)fileName:要下载文件在服务器端的物理路径
(2)page:调用本方法的页面的实例化对象。
针对其他语言基本实现大致相同。
//http文件下载
public static void FileDownMethod(string fileName,Page page)
{
if (File.Exists(fileName))
{
FileInfo DownloadFile = new FileInfo(fileName);
FileStream stream = new FileStream(fileName, FileMode.Open,
FileAccess.Read, FileShare.Read);
try
{
page.Response.Charset = "GB2312";
page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
page.Response.ContentType = "application/octet-stream";
page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(DownloadFile.Name));
int bufSize = (int)stream.Length;
byte[] buf = new byte[bufSize];
int bytesRead = stream.Read(buf, 0, bufSize);
page.Response.OutputStream.Write(buf, 0, bytesRead);
page.Response.End();
}
finally
{
stream.Close();
}
}
else
{
MyClientScript.ygJScript.Alert("服务器找不到该文件!", page);
}
}