Lets say, you've uploaded a pdf file, create a HyperLink in your page and give its NavigateUrl property the path to the file where it is stored like this
You can also assign the NavigateUrl property through codebehind. Read the file path and extension in a DataReader and for the specifc row assign ito the hyperlink like
protected void btnTimeLap_Click(object sender, EventArgs e)
{
string filename = @"Specify the file path in the server over here....";
FileInfo fileInfo = new FileInfo(filename);
Well you have done a greate job as you have stored the file path. If it is a physical path i.e. "C/WebPath/Folder/FileName.Ext"
Then just use the following code
Else use Server.MapPath(Path) to get the physical path
Use following Code to download the file
Protected Sub FileDownload(ByVal sender As Object, ByVal e As EventArgs) Handles btn_fileDownload.Click
Response.ContentType = "application/octet-stream"
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName)
Response.TransmitFile(Server.MapPath(filenameWithVirtualPath))
Response.End()
End Sub
By this code the file will download on the client end
Amit Srivastava
Senior Software Engineer
Find latest stuff on
DotnetAdda
zedric03
Member
14 Points
108 Posts
How can I download file from server to local machine using a filepath...
Oct 13, 2009 11:49 PM|LINK
Hi experts,
I have a feature in my project that able to save the uploaded file into the server pc's harddisk...
Now I want to retrieve it using only the filepath of the server where I uploaded it and save it through my local machine...
how can I achieve this?
I manage to save the filepath of the server to my database including the filename and file extension as a varchar...
also I can't use binary data for this for some reasons...
you response will be highly appreciated...
thanks in advance!
Visual Studio 2008
deepthoughts
Contributor
7288 Points
1051 Posts
Re: How can I download file from server to local machine using a filepath...
Oct 14, 2009 01:20 AM|LINK
Lets say, you've uploaded a pdf file, create a HyperLink in your page and give its NavigateUrl property the path to the file where it is stored like this
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Files/File1.pdf" Text="PDF File" />
You can also assign the NavigateUrl property through codebehind. Read the file path and extension in a DataReader and for the specifc row assign ito the hyperlink like
HyperLink1.NavigteUrl=Server.MapPath(dr["path"].ToString()+dr["extension"].ToString());
Hope it helps.
ramiramilu
All-Star
97923 Points
14516 Posts
Re: How can I download file from server to local machine using a filepath...
Oct 14, 2009 05:26 AM|LINK
Hello,
There are many possibilities for this.....
1)
<a href="specify the path here...">download</a>
2)
protected void btnTimeLap_Click(object sender, EventArgs e)
{
string filename = @"Specify the file path in the server over here....";
FileInfo fileInfo = new FileInfo(filename);
if (fileInfo.Exists)
{
Response.Clear();
Response.AddHeader("Content-Disposition", "inline;attachment; filename=" + fileInfo.Name);
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.Flush();
Response.WriteFile(fileInfo.FullName);
Response.End();
}
}
3)
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/1.doc">HyperLink</asp:HyperLink>
Hope this is helpful...
Thanks.
JumpStart
zedric03
Member
14 Points
108 Posts
Re: How can I download file from server to local machine using a filepath...
Oct 14, 2009 06:49 AM|LINK
thanks for the quick reply...
ill inform you if the codes worked for me...
amsneuton
Member
528 Points
121 Posts
Re: How can I download file from server to local machine using a filepath...
Oct 14, 2009 09:09 AM|LINK
Dear zedric03
Well you have done a greate job as you have stored the file path. If it is a physical path i.e. "C/WebPath/Folder/FileName.Ext"
Then just use the following code
Else use Server.MapPath(Path) to get the physical path
Use following Code to download the file
Protected Sub FileDownload(ByVal sender As Object, ByVal e As EventArgs) Handles btn_fileDownload.Click Response.ContentType = "application/octet-stream" Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName) Response.TransmitFile(Server.MapPath(filenameWithVirtualPath)) Response.End() End SubBy this code the file will download on the client end
Senior Software Engineer
Find latest stuff on
DotnetAdda
zedric03
Member
14 Points
108 Posts
Re: How can I download file from server to local machine using a filepath...
Oct 14, 2009 09:34 AM|LINK
a liitle help please...
I used this code:
HyperLink1.NavigteUrl=Server.MapPath(dr["path"].ToString()+dr["extension"].ToString());
how can I declare the "dr" and what assembly reference did you use to make it compiled... thanks!
zedric03
Member
14 Points
108 Posts
Re: How can I download file from server to local machine using a filepath...
Oct 14, 2009 09:36 AM|LINK
a liitle help please...
I used this code:
HyperLink1.NavigteUrl=Server.MapPath(dr["path"].ToString()+dr["extension"].ToString());
how can I declare the "dr" and what assembly reference did you use to make it compiled... thanks!
zedric03
Member
14 Points
108 Posts
Re: How can I download file from server to local machine using a filepath...
Oct 14, 2009 09:37 AM|LINK
amsneuton,
thanks for the code, but can you make it c# please.... just a favor...
deepthoughts
Contributor
7288 Points
1051 Posts
Re: How can I download file from server to local machine using a filepath...
Oct 14, 2009 09:51 AM|LINK
I meant you can read in the "path" and "extension" fields from your database and assign them to the NavigateUrl property of the HyperLink...
Thanks.
venkatu2005
All-Star
32487 Points
6742 Posts
Re: How can I download file from server to local machine using a filepath...
Oct 14, 2009 10:00 AM|LINK
Hi . dr - is the DataReader , which has getting the data from the database , if you want to assing the path manually assign it.
Thanks.