Click here to Skip to main content
13,201,050 members (68,538 online)
Rate this:
 
Please Sign up or sign in to vote.
my database contains following feilds:
autoid	        int	
pcode	    varchar(50)	
fyyear	    varchar(50)	
date	    varchar(50)	
salary	        int	
ta	        int	
contigency	int	
nrc	        int	
institcharges	int	
others	        int	
docname	    varchar(50)	
doctype	    varchar(50)	
docdata	    varbinary(MAX)

This is the code i am using to display the data in the button field:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Document Download")
        {
            int index = Convert.ToInt32(e.CommandArgument);
            string url = GridView1.Rows[index].Cells[13].Text;
            System.IO.FileInfo file = new System.IO.FileInfo(url);
            if (file.Exists)
            {
                Response.Clear();
                Response.AppendHeader("Content-Disposition:", "attachment; filename=" + file.Name);
                Response.AppendHeader("Content-Length", file.Length.ToString());
                Response.ContentType = "application/octet-stream";
                Response.TransmitFile(file.FullName);
                Response.End();
            }
            else
            {
                Response.Write("NO FILE PRESENT");
            }
        }
    }

but when i click the button field nothing happens
note: i have linked my button field to docname data field.
Posted 4-Feb-13 20:09pm
Updated 4-Feb-13 20:14pm
_Amy58.1K
v3
Comments
Sanjay K. Gupta 5-Feb-13 2:17am
   
add break point at
Response.TransmitFile(file.FullName);
and see the file.FullName value
a2ulthakur 5-Feb-13 2:22am
   
nothing in there ...i tried the breakpoints ..i just want a simple thing i want to download the file by its name (docname)from the button field in gridview my file is stored in database but i m not able to download it.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

There is two ways:
1st:
int index = Convert.ToInt32(e.CommandArgument);
string url = GridView1.Rows[index].Cells[13].Text;
Response.Redirect(url)

And
2nd:
int index = Convert.ToInt32(e.CommandArgument);
string url = GridView1.Rows[index].Cells[13].Text;
FileInfo file = new FileInfo(url);
if (file.Exists)
{
   Response.Clear();
   Response.ClearHeaders();
   Response.ClearContent();
   Response.AddHeader("content-disposition", "attachment; filename=" + fileName);
   Response.ContentType = "application/octet-stream";
   Response.AddHeader("Content-Length", file.Length.ToString());
   Response.WriteFile(file.FullName);
   Response.End();
}
else
{
   Response.Write("This file does not exist.");
}


Hope it works!
--Amit
  Permalink  
Comments
a2ulthakur 5-Feb-13 2:27am
   
string url = GridView1.Rows[index].Cells[13].Text; //in this line how do i identify cell 13. i wanna ask what does it signify ?is it the value of the cell where download button shd be ?
_Amy 5-Feb-13 2:32am
   
Cell 13 will contain the URL of the files which you want to download. The value of Cell 13 should be like:
~/SomeFolder/Somefile.docx
a2ulthakur 5-Feb-13 2:39am
   
but my files are on database ...they are not in any folder
a2ulthakur 5-Feb-13 4:08am
   
Could not find a part of the path 'C:\Users\cgrt\Desktop\MIS\~C\Desktop\MIS\docs\'. this is the error i m getting at this line

Response.TransmitFile(Server.MapPath("~C/Desktop/MIS/docs/" + fileName));
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Response.TransmitFile(Server.MapPath("~C/Desktop/MIS/docs/" + fileName));
i m getting error here "Could not find a part of the path 'C:\Users\cgrt\Desktop\MIS\~C\Desktop\MIS\docs\'."
  Permalink  
Comments
boogac 5-Feb-13 9:59am
   
when you write the full path ~ is needed ??

anyway,this is not an solution
a2ulthakur 5-Feb-13 13:42pm
   
ya i know this is not the solution.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web02 | 2.8.171020.1 | Last Updated 5 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100