I am trying to download the file from the repeater. Following is my code:
protected void DownloadFile(object sender, EventArgs e)
{
string filePath = (sender as LinkButton).CommandArgument;
Response.ContentType = ContentType;
Response.AppendHeader("Content-Disposition", "attachment; filename=" + HttpContext.Current.Server.MapPath("~/Data/") + filePath);
Response.WriteFile(filePath);
Response.End();
}
I am receiving the following exception:
The file is saved in the Data folder of the route folder:
How do I write the code to find the actual path?
看起来您的文件路径不正确(./College/Graphic1.jpg),与屏幕截图中显示的路径(./Data/Graphic1.jpg)不匹配
I solved my question. I changed my code to the following code:
protected void DownloadFile(object sender, EventArgs e)
{
WebClient req = new WebClient();
string filePath = (sender as LinkButton).CommandArgument;
Response.ContentType = ContentType;
HttpResponse response = HttpContext.Current.Response;
string file = Server.MapPath("~/Data/") + filePath;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
response.AddHeader("Content-Disposition", "attachment;filename="+filePath);
byte[] data = req.DownloadData(file);
response.BinaryWrite(data);
response.End();
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.