asp.net实现文件下载功能
2011-04-02 14:40
567 查看
今天又需要写程序,是网站下载功能。以前曾经有写,是写在一个类别中,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="下载视频" />
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();
}
今天Insus.NET只好帖出今天写的代码,仅供参考:
<asp:Button ID="ButtonDownload" runat="server" OnClick="ButtonButtonDownload_Click"
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();
}
相关文章推荐
- asp.net中多种实现文件下载功能
- asp.net中用FileStream类实现下载文件功能,自定义下载路径,像IE下载一样
- 在ASP.NET中实现压缩多个文件为.zip文件,实现批量下载功能
- 用C#(ASP.NET)怎么实现文件下载功能
- ASP .net 实现文件上传 下载功能
- ASP.NET中实现文件下载功能
- 拥有网页版小U盘 ASP.NET实现文件上传与下载功能
- Asp.net实现MVC处理文件的上传下载功能实例教程
- asp.net实现文件下载功能
- asp.net中实现文件下载功能
- Asp.net利用一般处理程序实现文件下载功能
- asp.net中实现文件下载功能
- asp.net实现多个文件同时下载功能
- asp.net中实现文件下载功能
- asp.net mvc导出excel文件先保存到服务器某个目录,然后再导出到客户端供客户下载功能的实现
- 在ASP.NET中实现压缩多个文件为.zip文件,实现批量下载功能
- asp.net中实现文件下载功能
- asp.net中实现文件下载功能
- ASP.NET中实现文件下载功能
- 拥有网页版小U盘 ASP.NET实现文件上传与下载功能