C# aspx文件下载

来源:互联网 发布:淘宝 云客服 工资 编辑:程序博客网 时间:2024/04/30 08:29
using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.IO;public partial class DownloadFiles : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { } /// /// 文件下载 /// /// 文件的完整路径 private void FileDownload(string FullFileName) { // 设置保存文件格式(example: *.xls): //Response.ContentType = "application/ms-excel"; FileInfo DownloadFile = new FileInfo(FullFileName); Response.Clear(); Response.ClearHeaders(); Response.Buffer = false; Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8)); Response.AppendHeader("Content-Length", DownloadFile.Length.ToString()); Response.WriteFile(DownloadFile.FullName); Response.Flush(); Response.End(); } protected void ButDownload1_Click(object sender, EventArgs e) { FileDownload(@"D:/DownloadFile/水电表资料汇编.rar"); } protected void ButCancel_Click(object sender, EventArgs e) { Response.Redirect("Main.aspx"); }}