954,593 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Downloading a file

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.

vidbee
Newbie Poster
4 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

Use forms authentication to restrict user rights. Then use one function to download file. What exactly do you need?

ManicCW
Junior Poster in Training
95 posts since Nov 2005
Reputation Points: 13
Solved Threads: 11
 
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.

vidbee
Newbie Poster
4 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

Here is my function for downloading file:

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)
                .Response.End()
            End If
        End With
    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 willforce download.

Hope it helps.

ManicCW
Junior Poster in Training
95 posts since Nov 2005
Reputation Points: 13
Solved Threads: 11
 

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?

vidbee
Newbie Poster
4 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

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.

ManicCW
Junior Poster in Training
95 posts since Nov 2005
Reputation Points: 13
Solved Threads: 11
 

It worked. Thank you so much!

vidbee
Newbie Poster
4 posts since Apr 2007
Reputation Points: 10
Solved Threads: 0
 

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.

pandiyarajan
Newbie Poster
4 posts since May 2007
Reputation Points: 10
Solved Threads: 1
 

Where did you put this function and how did you call it?

ManicCW
Junior Poster in Training
95 posts since Nov 2005
Reputation Points: 13
Solved Threads: 11
 

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

pandiyarajan
Newbie Poster
4 posts since May 2007
Reputation Points: 10
Solved Threads: 1
 

in case anyone ever wants it, there is an excellent example of this on the photo gallery in the Personal Website Starrter Kit

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
 

pandiyarajan maybe you have some bug or something that is causing aspnet_wp.exe to crash.

ManicCW
Junior Poster in Training
95 posts since Nov 2005
Reputation Points: 13
Solved Threads: 11
 

Thank you, It works for MS excel, Zip file. But still same for the exe files.

pandiyarajan
Newbie Poster
4 posts since May 2007
Reputation Points: 10
Solved Threads: 1
 

Then it probably exe file problem.
I will check MSDN if it has some help.
I never had need to upload or download exe file on the web. :D

ManicCW
Junior Poster in Training
95 posts since Nov 2005
Reputation Points: 13
Solved Threads: 11
 

Got the solution from http://support.microsoft.com/kb/812406

The file size is huge so webserver stops

pandiyarajan
Newbie Poster
4 posts since May 2007
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You