0
以下代碼段不起作用。我想從鏈接按鈕點擊事件的文件夾中下載一個CSV文件。在點擊鏈接按鈕不起作用的事件下載CSV文件
protected void LinkButton1_Click(object sender, EventArgs e)
{
string filePath = "~/Data/Book1.csv";
System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath(filePath));
if (file.Exists)
{
WebClient req = new WebClient();
HttpResponse response = HttpContext.Current.Response;
//string filePath = "";
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
response.AddHeader("Content-Disposition", "attachment;filename=Filename.extension");
byte[] data = req.DownloadData(Server.MapPath(filePath));
response.BinaryWrite(data);
response.End();
}
}
'Response.AppendHeader(「Content-Disposition」,「Attachment; Filename =」+ file.Name +「」);' –
您可以嘗試使用我發佈的代碼。 –
「不工作」是什麼意思?你有例外嗎?文件沒有傳輸到客戶端嗎?你爲什麼使用'WebClient'來讀取文件? – Markus