关于“下载”的源码
我想要一个.NET关于“下载”的源代码,简单实用--------------------编程问答-------------------- 关于下载?
什么意思? --------------------编程问答-------------------- 难道是传说中的“下载”··· --------------------编程问答--------------------
--------------------编程问答--------------------
/// <summary>
/// 下载文件
/// </summary>
/// <param name="path">文件路径</param>
public static void DownLoad(string path)
{
System.IO.FileInfo fi = new System.IO.FileInfo(path);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.Buffer = false;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(path)));
HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString());
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.WriteFile(path);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
#region 下载服务器上的文件--------------------编程问答--------------------
/// <summary>
/// 下载服务器上的文件
/// </summary>
/// <param name="PageResponse">程序中可以设置参数:HttpResponse ht=Page.Response;</param>
/// <param name="serverPath">服务器上的文件路径</param>
public void DownloadFile(HttpResponse response, string serverPath)
{
FileStream fs = null;
try
{
fs = File.OpenRead(serverPath);
byte[] buffer = new byte[1024];
long count = 1024;
response.Buffer = true;
response.AddHeader("Connection", "Keep-Alive");
response.ContentType = "application/octet-stream";
response.AddHeader("Content-Disposition", "attachment;filename=" + Path.GetFileName(serverPath));//下载时要保存的默认文件名
response.AddHeader("Content-Length", fs.Length.ToString());
while (count == 1024)
{
count = fs.Read(buffer, 0, 1024);
response.BinaryWrite(buffer);
}
}
catch
{
}
finally
{
fs.Close();
}
}
#endregion
ding
--------------------编程问答--------------------
补充:.NET技术 , ASP.NET