Below is my html Content
<body>
<form id="form1" runat="server">
<asp:FileUpload ID="ful" runat="server" /> <asp:Button ID="btnupload" runat="server" Text="Upload" onclick="btnupload_Click" /><br />
<asp:Label ID="lblmsg" runat="server" Visible="false" Font-Bold="true" ForeColor="CadetBlue"> </asp:Label><br />
<asp:Button ID="btnshow" runat="server" Text="ShowAllFiles"
onclick="btnshow_Click" /><br /><br /> <asp:GridView ID="gv" runat="server" AutoGenerateColumns="false" GridLines="Horizontal" AlternatingRowStyle-BackColor="DarkViolet" DataKeyNames="DocId" onrowcommand="gv_RowCommand">
<Columns> <asp:BoundField HeaderText="DocId" DataField="DocId"/> <asp:BoundField HeaderText="DocumentName" DataField="DocName" />
<asp:TemplateField> <ItemTemplate> <asp:ImageButton ID="img" runat="server" ImageUrl='<%#Eval("ImageUrl") %>' CommandName="Download" CommandArgument='<%#Eval("DocId") %>' /> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div>
Below is my code for Rowcommand event i.e. when i click on the Imagebutton
protected void gv_RowCommand(object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Download")
{
string filename = string.Empty;
int id = Convert.ToInt32(e.CommandArgument);
cmd = new SqlCommand("SELECT DocName,DocType FROM Documents WHERE DocID = " + id, cn);
cn.Open();
dr = cmd.ExecuteReader();
if (dr.Read())
{
filename = dr["DocName"].ToString();
Response.ContentType = dr["DocType"].ToString();
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
Response.TransmitFile(Server.MapPath("~/Docs/" + filename));
Response.End();
}
}
}
}
Initially the data is saved into database and then retrieved in Gridview.Only one problem I have noticed with my code is...when the filename contains spaces in between for eg:
Dot Net.jpg (or)
A B.doc & when i click on imagebutton,in the windows download popup the name is only
Dot(or) A (Net.jpg,B.doc is not appearing) and when i try to save the file it is saved with no extension how to solve this.
Thank you.