Downloading a file

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2007
Posts: 4
Reputation: vidbee is an unknown quantity at this point 
Solved Threads: 0
vidbee vidbee is offline Offline
Newbie Poster

Downloading a file

 
0
  #1
Apr 9th, 2007
I am writing an ASP.net application (with VB.net). Users are allowed to register and upload files. I want to allow them to download the files at a later point. How should I do this? Any help is appreciated.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 95
Reputation: ManicCW is an unknown quantity at this point 
Solved Threads: 11
ManicCW's Avatar
ManicCW ManicCW is offline Offline
Junior Poster in Training

Re: Downloading a file

 
0
  #2
Apr 10th, 2007
Use forms authentication to restrict user rights. Then use one function to download file. What exactly do you need?
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 4
Reputation: vidbee is an unknown quantity at this point 
Solved Threads: 0
vidbee vidbee is offline Offline
Newbie Poster

Re: Downloading a file

 
0
  #3
Apr 11th, 2007
Originally Posted by ManicCW View Post
Use forms authentication to restrict user rights. Then use one function to download file. What exactly do you need?

I have already done the authentication part. I now have the full path to the file on the web server in a string. I dont know what the code to download the file should be. I already tried some things from the Internet, but nothing has worked so far.

One other thing I tried was to have a download hyperlink. I set the navigate URL to the full path of the file. When I click on it, I want a pop-up with options to either save or open the file. I am dealing with only .txt, .doc and .pdf files.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 95
Reputation: ManicCW is an unknown quantity at this point 
Solved Threads: 11
ManicCW's Avatar
ManicCW ManicCW is offline Offline
Junior Poster in Training

Re: Downloading a file

 
0
  #4
Apr 11th, 2007
Here is my function for downloading file:

  1. Public Sub DownloadFile(ByVal path As String)
  2. With HttpContext.Current
  3. Dim file As IO.FileInfo = New IO.FileInfo(.Server.MapPath(path))
  4. If file.Exists Then
  5. .Response.Clear()
  6. .Response.AddHeader("Content-Disposition", "attachment; filename=" & file.Name)
  7. .Response.AddHeader("Content-Length", file.Length.ToString())
  8. .Response.ContentType = "application/octet-stream"
  9. .Response.WriteFile(file.FullName)
  10. .Response.End()
  11. End If
  12. End With
  13. End Sub

Put this function in some module or class. Make sure it is public.
Just pass path to function. :cheesy:
And the cool thing is that it will force download.

Hope it helps.
Last edited by ManicCW; Apr 11th, 2007 at 10:44 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 4
Reputation: vidbee is an unknown quantity at this point 
Solved Threads: 0
vidbee vidbee is offline Offline
Newbie Poster

Re: Downloading a file

 
0
  #5
Apr 12th, 2007
I tried this code. But I am getting the following errors:

'C:\AddlDocs\vidhyavee_1imp.txt' is not a valid virtual path.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: 'C:\AddlDocs\vidhyavee_1imp.txt' is not a valid virtual path.

Source Error:


Line 151: Public Sub DownloadFile(ByVal path As String)
Line 152: With HttpContext.Current
Line 153: Dim file As IO.FileInfo = New IO.FileInfo(.Server.MapPath(path))
Line 154: If file.Exists Then
Line 155: .Response.Clear()

Tha path variable should be containing the complete path to the file, including the file name, right? Also, I am using the ASP development server from the Web Developer's environment to test my application. Should I be doing something else? Is there something wrong with my browser settings?

One other thing I tried was to have a hyperlink with the navigate URL set to the complete file path. It doesnt download the file automatically, but I am able to save the file by doing a 'save target as'. Do you think I should just proceed this way?
Last edited by vidbee; Apr 12th, 2007 at 10:11 am. Reason: Changed font color
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 95
Reputation: ManicCW is an unknown quantity at this point 
Solved Threads: 11
ManicCW's Avatar
ManicCW ManicCW is offline Offline
Junior Poster in Training

Re: Downloading a file

 
1
  #6
Apr 13th, 2007
The error is very clear. You are passing absolute path instead of relative. Pass string like "~/MyFiles/MyFile.doc". If you want to use absolute path then fix code like this:

Instead of:

Dim file As IO.FileInfo = New IO.FileInfo(.Server.MapPath(path))

Put this:

Dim file As IO.FileInfo = New IO.FileInfo(path)


Hope it helps.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 4
Reputation: vidbee is an unknown quantity at this point 
Solved Threads: 0
vidbee vidbee is offline Offline
Newbie Poster

Re: Downloading a file

 
0
  #7
Apr 13th, 2007
It worked. Thank you so much!
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4
Reputation: pandiyarajan is an unknown quantity at this point 
Solved Threads: 1
pandiyarajan pandiyarajan is offline Offline
Newbie Poster

Re: Downloading a file

 
0
  #8
May 7th, 2007
Thanks for giving the code to download the file.

But when I am using in my application. i am getting the error like this
"Server Application Unavailable"
Upon seeing the event log it says
"aspnet_wp.exe stopped unexpectedly". do you know why it is stopping.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 95
Reputation: ManicCW is an unknown quantity at this point 
Solved Threads: 11
ManicCW's Avatar
ManicCW ManicCW is offline Offline
Junior Poster in Training

Re: Downloading a file

 
0
  #9
May 7th, 2007
Where did you put this function and how did you call it?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4
Reputation: pandiyarajan is an unknown quantity at this point 
Solved Threads: 1
pandiyarajan pandiyarajan is offline Offline
Newbie Poster

Re: Downloading a file

 
0
  #10
May 8th, 2007
Hai

I am using like this, Calling as a function from the linkbutton click event. I am writing in the code behind.

Private Sub lnkDownload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lnkDownload.Click
Call DownloadFile("Downloads/xyz.exe")
End Sub

Public Sub DownloadFile(ByVal path As String)
With HttpContext.Current
Dim file As IO.FileInfo = New IO.FileInfo(.Server.MapPath(path))
If file.Exists Then
.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.ToString)
End If
End With
End Sub
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC