c#下載伺服器資料夾目錄下的檔案
阿新 • • 發佈:2019-01-03
首先,在Page_Load事件中新增如下程式碼:
LinkButton l = new LinkButton();
l.Text = "點此下載檔案" ;
l.ToolTip = 檔名
l.Font.Bold = true;
l.Click += new EventHandler(l_Click);
this.PlaceHolder1.Controls.Add(l);
接下來,實現Click 事件:
void l_Click(object sender, EventArgs e)
{
LinkButton l = sender as LinkButton;
string filePath = Server.MapPath("~/UpLoad") + "//" + l.ToolTip;
System.IO.FileInfo file = new System.IO.FileInfo(filePath);
if (file.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Filter.Close();
Response.WriteFile(file.FullName);
Response.End();
}
}