using BLL; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace FineUIPro.Web { public partial class FileDown : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void butDown_Click(object sender, EventArgs e) { if (txtFileName.Text != "") { download(txtFileName.Text.Trim()); } else { Alert.Show("请输入下载的文件名!"); } } private void download(string fileName) { System.IO.Stream iStream = null; byte[] buffer = new Byte[10000];//设置大小 int length; long dataToRead; string rootPath = Server.MapPath("~/") + "FileUpload\\HJGL\\"; string filepath = rootPath + fileName;//文件的路径 string filename = System.IO.Path.GetFileName(filepath); try { iStream = new System.IO.FileStream(filepath, FileMode.Open, FileAccess.Read); dataToRead = iStream.Length; Response.Clear(); Response.ClearHeaders(); Response.ClearContent(); Response.ContentType = "application/unknow"; Response.AddHeader("Content-Length", dataToRead.ToString()); Response.AddHeader("Content-Disposition", "attachment; filename=" + filename); while (dataToRead > 0) { if (Response.IsClientConnected) { length = iStream.Read(buffer, 0, 10000); Response.OutputStream.Write(buffer, 0, length); Response.Flush(); buffer = new Byte[10000]; dataToRead = dataToRead - length; } else { dataToRead = -1; } } } catch (Exception ex) { Response.Write("Error : " + ex.Message); } finally { if (iStream != null) { iStream.Close(); } Response.End(); } } } }