简体   繁体   中英

Can't download a file from application folder in asp.net

I have a grid view. The data bound to the grid view. I have a column with download link button. When I click on download the file should be downloaded but I got some issue while tying it.

I uploaded files to application folder and the path to database but when I try to download the file it fires an error

    LinkButton lnkbtn = sender as LinkButton;
    GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
    string filePath = gridView1.DataKeys[gvrow.RowIndex].Value.ToString();

    Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\"");
    Response.TransmitFile(Server.MapPath(filePath));
    Response.End();

But my application folder has the uploaded file

在此处输入图片说明

Your file path is wrong. Server is looking for the file in the site root, here: C:\\Users\\lagis\\Downloads\\templgarden\\jhvgdfjka.txt . But it seems your desired file is in /documents/mini . So, you need to use the right path(url) for Server.TransmitFile like this:

Response.TransmitFile(Server.MapPath("/documents/mini/" + filePath));

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM