asp.net 問題,一個button實現文件的下載

網絡上找的,都有點複雜。。。。。。。 

//下載功能
public void DownLoad(string pathName)
{
string FilePath = Server.MapPath(pathName);
if (System.IO.File.Exists(FilePath))
{
FileInfo file = new FileInfo(FilePath);
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); //解決中文文件名亂碼
Response.AddHeader("Content-length", file.Length.ToString());
Response.ContentType = "appliction/octet-stream";
Response.WriteFile(file.FullName);
Response.End();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DownLoad("~/文檔/學習.txt");
}

 

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetButton();
}
}
//將下載按鈕的button的id傳給SetBtn方法設置下載事件
private void GetButton()
{
SetBtn(Button1.ID);
SetBtn(Button2.ID);
SetBtn(Button3.ID);
SetBtn(Button4.ID);
}
private void SetBtn(string btnID)
{
if (btnID != "")
{
try
{
Button btn = (Button)FindControl(btnID);
btn.Click += new System.EventHandler(this.btn_Click);
}
catch (Exception)
{
//不是button
}
}
}
protected void btn_Click(object sender, EventArgs e)
{
//判斷下載的文件是否存e69da5e887aae79fa5e9819331333264626635在
if (System.IO.File.Exists(Server.MapPath("123.txt")))
{
string filePath = Server.MapPath("123.txt");
FileInfo DownloadFile = new FileInfo(filePath);
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = false;
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(DownloadFile.Name, System.Text.Encoding.UTF8));
System.Web.HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
System.Web.HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.End();
}
}

以下轉自:https://blog.csdn.net/codeshark/article/details/2473664
ASP.NET實現文件下載

 

方式一:TransmitFile實現下載。將指定的文件直接寫入 HTTP 響應輸出流,而不在內存中緩衝該文件。

protected void Button1_Click(object sender, EventArgs e)
{
   /*
     微軟爲Response對象提供了一個新的方法TransmitFile來解決使用Response.BinaryWrite下載超過400mb的文件時導致Aspnet_wp.exe進程回收而無法成功下載的問題。
代碼如下:
*/
    Response.ContentType = "application/x-zip-compressed";
    Response.AddHeader("Content-Disposition", "attachment;filename=CodeShark.zip");
    string filename = Server.MapPath("DownLoad/CodeShark.zip");
    Response.TransmitFile(filename);
}

方式二:WriteFile實現下載,將指定的文件直接寫入 HTTP 響應輸出流。注意:對大型文件使用此方法時,調用此方法可能導致異常。可以使用此方法的文件大小取決於 Web 服務器的硬件配置。

protected void Button2_Click(object sender, EventArgs e)
{
    string fileName = "CodeShark.zip";//客戶端保存的文件名
string filePath = Server.MapPath("DownLoad/CodeShark.doc");//路徑
FileInfo fileInfo = new FileInfo(filePath);
    Response.Clear();
    Response.ClearContent();
    Response.ClearHeaders();
    Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
    Response.AddHeader("Content-Length", fileInfo.Length.ToString());
    Response.AddHeader("Content-Transfer-Encoding", "binary");
    Response.ContentType = "application/octet-stream";
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
    Response.WriteFile(fileInfo.FullName);
    Response.Flush();
    Response.End();
}

方式三:WriteFile分塊下載

protected void Button3_Click(object sender, EventArgs e)
{
    string fileName = "CodeShark.zip";//客戶端保存的文件名
string filePath = Server.MapPath("DownLoad/CodeShark.zip");//路徑
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
    if (fileInfo.Exists == true)
    {
        const long ChunkSize = 102400;//100K 每次讀取文件,只讀取100K,這樣可以緩解服務器的壓力
  byte[] buffer = new byte[ChunkSize];
        Response.Clear();
        System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
        long dataLengthToRead = iStream.Length;//獲取下載的文件總大小
  Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
        while (dataLengthToRead > 0 && Response.IsClientConnected)
        {
            int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//讀取的大小
    Response.OutputStream.Write(buffer, 0, lengthRead);
            Response.Flush();
            dataLengthToRead = dataLengthToRead - lengthRead;
        }
        Response.Close();
    }
}

方式四:流方式下載

 protected void Button4_Click(object sender, EventArgs e)
{
    string fileName = "CodeShark.zip";//客戶端保存的文件名
string filePath = Server.MapPath("DownLoad/CodeShark.zip");//路徑
//以字符流的形式下載文件
FileStream fs = new FileStream(filePath, FileMode.Open);
    byte[] bytes = new byte[(int)fs.Length];
    fs.Read(bytes, 0, bytes.Length);
    fs.Close();
    Response.ContentType = "application/octet-stream";
    //通知瀏覽器下載文件而不是打開
Response.AddHeader("Content-Disposition", "attachment;   filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章