asp.net实现文件下载功能
今天又需要写程序,是网站下载功能。以前曾经有写,是写在一个类别中,http://www.cnblogs.com/insus/articles/1411756.html 也录制过视频,http://www.cnblogs.com/insus/articles/1411761.html 不过还是有网友向Insus.NET索求代码。
今天Insus.NET只好帖出今天写的代码,仅供参考:
<asp:Button ID="ButtonDownload" runat="server" OnClick="ButtonButtonDownload_Click"
Text="下载视频" />
Text="下载视频" />
xxx.aspx.cs:
View Code
protected void ButtonButtonDownload_Click(object sender, EventArgs e)
{
HttpResponse _Response = HttpContext.Current.Response;
//取数据表
DataRow objDataRow = objMedia.GetFileByPrimaryKey(id).Rows[0];
//取得文件目录与文件名。
string f = objDataRow["Directory"].ToString() + objDataRow["NewFileName"].ToString();
//取得旧文件名
string fn = objDataRow["OldFileName"].ToString();
FileInfo fileInfo = new FileInfo(Server.MapPath (f));
_Response.Clear();
_Response.ClearHeaders();
_Response.Buffer = false;
_Response.ContentType = "application/octet-stream";
_Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fn, System.Text.Encoding.UTF8));
_Response.AppendHeader("Content-Length", fileInfo.Length.ToString());
_Response.WriteFile(fileInfo.FullName);
_Response.Flush();
_Response.End();
}
{
HttpResponse _Response = HttpContext.Current.Response;
//取数据表
DataRow objDataRow = objMedia.GetFileByPrimaryKey(id).Rows[0];
//取得文件目录与文件名。
string f = objDataRow["Directory"].ToString() + objDataRow["NewFileName"].ToString();
//取得旧文件名
string fn = objDataRow["OldFileName"].ToString();
FileInfo fileInfo = new FileInfo(Server.MapPath (f));
_Response.Clear();
_Response.ClearHeaders();
_Response.Buffer = false;
_Response.ContentType = "application/octet-stream";
_Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fn, System.Text.Encoding.UTF8));
_Response.AppendHeader("Content-Length", fileInfo.Length.ToString());
_Response.WriteFile(fileInfo.FullName);
_Response.Flush();
_Response.End();
}