Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
when i try to add linkbutton and want to download file then it shows me error
code
C#
protected void lnkdownload_Click(object sender, EventArgs e)
        {

            Response.Clear();
            LinkButton l1 = (LinkButton)sender;
            string filenm = l1.CommandArgument;

            Response.ContentType = ContentType;
            Response.AddHeader("Content-Disposition", "attachment; filename=\"" + Path.GetFileName(filenm) + "\"");
            Response.Charset = "";
            Response.WriteFile(filenm);
            Response.End();
        }


error
the path does not exist in current context
Posted
Comments
Maciej Los 24-Feb-14 4:03am    
Diya Ayesa 24-Feb-14 4:12am    
ok now i try this
protected void LinkButton1_Click(object sender, EventArgs e)
{

Response.Clear();
LinkButton l1 = (LinkButton)sender;
string filenm = l1.CommandArgument;
string Path = MapPath("~/Docfiles/" + filenm);
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=" + filenm);
Response.BinaryWrite(bts);
Response.Flush();
Response.End();
}

but when on file it does not download
ravikhoda 24-Feb-14 4:40am    
what is the value of filenm ? also check if that file is physically available on that location inside Docfiles folder ?

1 solution

To download a file on button click , Kindly have a look on a little trick posted by me on the following link.

File Download on Image Button or Button Click


hope it will help. :)
 
Share this answer
 

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