Click here to Skip to main content
15,907,492 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to download file from gridview link button uploaded by respective user but getting exception



'E:/Latest_jobportal/jvportal/Uploads/purushottam.kumar/purushottam.kumarCV.docx' is a physical path, but a virtual path was expected.


my code is as follows


C#
if (e.CommandName == "cmd")
        {
            string filename = e.CommandArgument.ToString();
            if (filename != "")
            {
                string path = MapPath(filename);
                byte[] bts = System.IO.File.ReadAllBytes(path);
                Response.Clear();
                Response.ClearHeaders();
                Response.AddHeader("Content-Type", "Application/octet-stream");
                Response.AddHeader("Content-Length", bts.Length.ToString());

                Response.AddHeader("Content-Disposition", "attachment;   filename=" + filename);

                Response.BinaryWrite(bts);

                Response.Flush();

                Response.End();
            }
}



}
Posted
Comments
Member 10310320 7-May-14 5:26am    
Can any one Help me??

In cs file add following

C#
protected void DownloadFile(object sender, EventArgs e)
   {
       string filePath = (sender as LinkButton).CommandArgument;
       Response.ContentType = ContentType;
       Response.AppendHeader("Content-Disposition", "attachment; filename=" + Path.GetFileName(filePath));
       Response.WriteFile(filePath);
       Response.End();
   }


In aspx file add Templatefiled in Columns in Gridview

XML
<asp:TemplateField HeaderText="Download" HeaderStyle-Width="7%">
                  <ItemTemplate>
                      <asp:LinkButton ID="lnkDownload" Text="Download" CommandArgument='<%# Eval("FilePath") %>' runat="server" OnClick="DownloadFile"></asp:LinkButton>
                  </ItemTemplate>
              </asp:TemplateField>
 
Share this answer
 
Comments
Member 10310320 7-May-14 4:56am    
Do I have to remove my previous code from rowCommand event?????
Member 10310320 7-May-14 5:22am    
No this code is not working.....
Darshan.Pa 7-May-14 5:33am    
it is not like that, you copy and paste code given and it will run.
you have to change code as per your requirement. I can give you solution not exact code as per your requirement
Member 10310320 7-May-14 5:52am    
I have used this code according to my requirement this time i am not getting any exception....but my problem remain the same file is not downloading..
Darshan.Pa 7-May-14 7:11am    
make sure value coming fom database is like for ex. "~/docs/041609-1145.TXT".
since you already has real path, you don't need MapPath
C#
byte[] bts = System.IO.File.ReadAllBytes(filename);
 
Share this answer
 
v2
Comments
Member 10310320 7-May-14 5:35am    
I removed MapPath still its not working

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