Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code behind.
C#
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
       {
           if (e.CommandName == "Download")
           {
               string Filepath = e.CommandArgument.ToString();
               string path = MapPath(Filepath);
               byte[] bts = System.IO.File.ReadAllBytes(path);


               Response.Clear();
               Response.ClearHeaders();
               Response.AddHeader("Content-type", "application/octect-stream");
               Response.AddHeader("Content-Length", bts.Length.ToString());
               Response.AddHeader("Content-Disposition", "attachment; Filepath=" + Filepath);
               Response.BinaryWrite(bts);
               Response.Flush();
               Response.End();
           }
       }

But when I try downloading, I get this error "' is a physical path, but a virtual path was expected", can anybody help me with this??
Posted
Updated 28-Jul-14 0:43am
v2

1 solution

I think your error is in Mapping the path. You did MapPath(Filepath) already. So, Filepath is already a local path, but you pass it to a method that expects a virtual path to convert.


  --Amy
 
Share this answer
 
Comments
ErikVabo 28-Jul-14 6:55am    
If I delete MapPath it works! There is only one thing, all the files I download are named "downloadfile.aspx", "downloadfile.aspx (1)" and the files are originally pdf files, so in order of them to work I need to change the name from .aspx to .pdf, is there anyway I can avoid this? So the filename and type is alike the file I download?
_Amy 28-Jul-14 7:04am    
Try this:
Response.AddHeader("Content-Disposition", "attachment; filename=FileName.pdf; Filepath=" + Filepath);
ErikVabo 28-Jul-14 7:58am    
All the files are now called FileName.pdf, but forgot to say, it is not only pdf files, det are different types such as .tiff
_Amy 28-Jul-14 8:02am    
Then try this:
Response.AddHeader("Content-Disposition", "attachment; filename="+Path.GetFileName(path)+"; Filepath=" + Filepath);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900