| | |
Downloading a file
Please support our ASP.NET advertiser: Intel Parallel Studio Home
Thread Solved |
Use forms authentication to restrict user rights. Then use one function to download file. What exactly do you need?
•
•
Join Date: Apr 2007
Posts: 4
Reputation:
Solved Threads: 0
•
•
•
•
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.
Here is my function for downloading file:
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.
ASP.NET Syntax (Toggle Plain Text)
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 will force download.
Hope it helps.
Last edited by ManicCW; Apr 11th, 2007 at 10:44 am.
•
•
Join Date: Apr 2007
Posts: 4
Reputation:
Solved Threads: 0
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?
'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
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.
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.
•
•
Join Date: May 2007
Posts: 4
Reputation:
Solved Threads: 1
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
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
![]() |
Similar Threads
- downloading a file (C++)
- Downloading a File from Server using JavaScript (JavaScript / DHTML / AJAX)
- Problems downloading Python (Python)
- Howdo I download a .mim file in Macintosh (OS X)
Other Threads in the ASP.NET Forum
- Previous Thread: help with detals view, looking up data from another table
- Next Thread: urgent please>>>
| Thread Tools | Search this Thread |
.net activexcontrol advice ajax alltypeofvideos appliances asp asp.net bc30451 beginner bottomasp.net box browser button c# c#gridviewcolumn cac checkbox click commonfunctions confirmationcodegeneration content courier css dataaccesslayer database datagridview datagridviewcheckbox datalist deadlock development dgv dialog dropdownlist dynamically edit expose fileuploader fill flash formatdecimal forms formview gridview gudi homeedition iframe iis javascript jquery listbox login microsoft mono mouse mssql multistepregistration news numerical objects opera panelmasterpagebuttoncontrols radio redirect registration relationaldatabases reportemail rotatepage save schoolproject search security sessionvariables silverlight smartcard smoobjects software sql-server sqlserver2005 suse textbox tracking treeview unauthorized validatedate validation vb.net video videos virtualdirectory vista visualstudio web webapplications webdevelopemnt webprogramming webservice xml xsl youareanotmemberofthedebuggerusers





