Protected Sub GVReceivedDocuments_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GVReceivedDocuments.RowCommand
If e.CommandName = "DownloadTheFile" Then
Dim btnDownloadTheFile As Button = DirectCast(e.CommandSource, Button)
Dim gvr As GridViewRow = DirectCast(btnDownloadTheFile.NamingContainer, GridViewRow)
Dim fileInfo() As String = e.CommandArgument.ToString().Split(";")
Dim FileName As String = fileInfo(1)
Dim FullPath As String = fileInfo(0)
Downloadfile(FileName, FullPath)
end if
End Sub
with IE i can File Download Dailog box and i can either save or open the file.
but with Firefox this code doesn't work.
with Firefox too, i get the file download dialog box. but i can't open the downloaded file because file type is not recgonised
ClientScript.RegisterStartupScript(Type.GetType("System.String"),
"messagebox",
"<script type=\"text/javascript\">alert('File Does Not Exists');</script>");
MSTC ,.NET Developer LINKdotNET - LINK Development
Don't forget to Mark As Answer on the post that helped you. It encourages them to share their knowledge, and it helps others to easily identify the solution.
Garry Meax
Member
142 Points
63 Posts
Download file from GridView
Apr 30, 2009 04:12 PM|LINK
this is my grid view
<asp:GridView ID="GVReceivedDocuments" runat="server"
DataKeyNames="EntreeInDocID"
AutoGenerateColumns="False"
AlternatingRowStyle-BackColor="#A0A0A0" AllowPaging="True">
<PagerStyle CssClass="cssPager" />
<Columns>
<asp:BoundField HeaderText="From" DataField="SenderName" >
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="Document Type" DataField="DocumentType" >
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField HeaderText="Download">
<ItemTemplate>
<asp:Button ID="btnDownloadTheFile" runat="server" CommandArgument='<%#Eval("Path") +";" + Eval("Path") %>' commandname="DownloadTheFile" Text="Download" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
this is my code behid
Protected Sub GVReceivedDocuments_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GVReceivedDocuments.RowCommand
If e.CommandName = "DownloadTheFile" Then
Dim btnDownloadTheFile As Button = DirectCast(e.CommandSource, Button)
Dim gvr As GridViewRow = DirectCast(btnDownloadTheFile.NamingContainer, GridViewRow)
Dim fileInfo() As String = e.CommandArgument.ToString().Split(";")
Dim FileName As String = fileInfo(1)
Dim FullPath As String = fileInfo(0)
Downloadfile(FileName, FullPath)
end if
End Sub
with IE i can File Download Dailog box and i can either save or open the file.
but with Firefox this code doesn't work.
with Firefox too, i get the file download dialog box. but i can't open the downloaded file because file type is not recgonised
WarCry
Participant
852 Points
148 Posts
Re: Download file from GridView
Apr 30, 2009 09:03 PM|LINK
try something like that
in code behind
//Download Method
protected void DownloadFile(string name){
string _path = Request.PhysicalApplicationPath + "Public\\DownloadFiles" + name;System.IO.
FileInfo _file = new System.IO.FileInfo(_path); if (_file.Exists){
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + _file.Name);Response.AddHeader(
"Content-Length", _file.Length.ToString()); Response.ContentType = "application/octet-stream";Response.WriteFile(_file.FullName);
Response.End();
}
else{
ClientScript.RegisterStartupScript(Type.GetType("System.String"), "messagebox", "<script type=\"text/javascript\">alert('File Does Not Exists');</script>");}
}
//Event Handelar
protected void LinkButton1_Click(object sender, EventArgs e){
string _Name = ((LinkButton)sender).Text;DownloadFile(_Name);
}
in gridview Markup
<
asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click" Text='<%# Eval("FilePath", "{0}") %>'></asp:LinkButton>good luck
MSTC ,.NET Developer
LINKdotNET - LINK Development
Don't forget to Mark As Answer on the post that helped you. It encourages them to share their knowledge, and it helps others to easily identify the solution.
Ram Reddy Me...
Star
9604 Points
1314 Posts
Re: Download file from GridView
May 01, 2009 04:24 AM|LINK
Check my post for the same here.
Abhiram Reddy Mekha
Garry Meax
Member
142 Points
63 Posts
Re: Download file from GridView
May 01, 2009 06:22 AM|LINK
can you tell me why this works in IE but not in Firefox
Garry Meax
Member
142 Points
63 Posts
Re: Download file from GridView
May 01, 2009 06:44 AM|LINK
now i have found out that, if a file name is a single word then it works both in IE and Firefox.
however if file name is some thing like this "Contact Details" then firefox doesn't recgonise the file type with this code.
Garry Meax
Member
142 Points
63 Posts
Re: Download file from GridView
May 01, 2009 07:05 AM|LINK
i guess i have to remove the white spaces from file name at the time file uploading
Mad-Halfling
Participant
1438 Points
729 Posts
Re: Download file from GridView
May 01, 2009 08:23 AM|LINK
Try replacing the spaces in the link with %20 - FF may be treating them as a separator
Garry Meax
Member
142 Points
63 Posts
Re: Download file from GridView
May 01, 2009 09:34 AM|LINK
here is the solution:
in data grid instead of binind full path of the file, no i am binind only file name
<asp:GridView ID="GVReceivedDocuments" runat="server"
DataKeyNames="EntreeInDocID"
AutoGenerateColumns="False"
AlternatingRowStyle-BackColor="#A0A0A0" AllowPaging="True">
<PagerStyle CssClass="cssPager" />
<Columns>
<asp:BoundField HeaderText="From" DataField="SenderName" >
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:BoundField HeaderText="Document Type" DataField="DocumentType" >
<ItemStyle HorizontalAlign="Center" />
<HeaderStyle HorizontalAlign="Center" />
</asp:BoundField>
<asp:TemplateField HeaderText="Download">
<ItemTemplate>
<asp:Button ID="btnDownloadTheFile" runat="server" CommandArgument='<%#Eval("FileName") %>' commandname="DownloadTheFile" Text="Download" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
when i upload the file to server i remove the white space from the posted file name using this code.
FileNameWithSpaces = myFile.FileName
FileNameWithOutSpaces = FileNameWithSpaces.replace(" ", "")
myFile.PostedFile.SaveAs(path & _
FileNameWithOutSpaces)
this is code behid code for gridview "download" button click
Protected Sub MyGV_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles MyGV.RowCommand
If e.CommandName = "DownloadTheFile" Then
Dim btnDownloadTheFile As Button = DirectCast(e.CommandSource, Button)
Dim gvr As GridViewRow = DirectCast(btnDownloadTheFile.NamingContainer, GridViewRow)
Dim fileInfo As String = e.CommandArgument.ToString()
Dim FileName As String = fileInfo
Downloadfile(FileName)
End If
End Sub
here is the function which actually downloads
Private Sub Downloadfile(ByVal fileName As String)
Response.Clear()
Response.AddHeader("Content-Disposition", "attachment; filename=" & fileName)
Response.ContentType = "application/octet-stream"
Response.TransmitFile("C:\Inetpub\wwwroot\MyWebSite\PublicFiles\Downloads\" & fileName)
Response.End()
End Sub
thanks you all for replying.
hope this helps someone.
hiral.hapani
Participant
1046 Points
206 Posts
Re: Download file from GridView
May 01, 2009 12:14 PM|LINK
hi,
there is no need to use link button for it.
You can use anchor link in which please give the full path in href where you gonna store that file.
Please give FULL PATH not just a file name. it will automatically give you open/save/cancel option dialogue box.
Please let me know if you have any query regarding this.
Hiral Hapani
'Action of Today Becomes Destiny of Tomorrow.'
venubabum.ne...
Member
12 Points
6 Posts
Re: Download file from GridView
Oct 13, 2009 06:48 AM|LINK
hi hiral.hapani
Pls give the syntax how anchor tag used in gridview downloas a file.
this is very urgent
Thansk&Regards
SWEETY