|
-
May 22nd, 2006, 03:04 AM
#1
Thread Starter
Frenzied Member
Download a file
I have some files saved to the server file system outside of wwwroot so they can not be accessed using a direct URL. Therefore I need another method of sending these files to a browser. Ideally a file download box should appear offering to open or save the file.
Could I have suggestions please?
DJ
If I have been helpful please rate my post. If I haven't tell me!
-
May 24th, 2006, 03:40 PM
#2
Re: Download a file
VB Code:
Dim FilePath As String = strFileName
Dim TargetFile As New System.IO.FileInfo(FilePath)
' clear the current output content from the buffer
Response.Clear()
' add the header that specifies the default filename for the Download/
' SaveAs dialog
Response.AddHeader("Content-Disposition", "attachment; filename=" + _
TargetFile.Name)
' add the header that specifies the file size, so that the browser
' can show the download progress
Response.AddHeader("Content-Length", TargetFile.Length.ToString())
' specify that the response is a stream that cannot be read by the
' client and must be downloaded
Response.ContentType = "application/octet-stream"
' send the file stream to the client
Response.WriteFile(TargetFile.FullName)
' stop the execution of this page
Response.End()
Place that in a page. Let the path of the file be determined by a querystring, apply your own logic there. Fine?
-
May 25th, 2006, 05:11 AM
#3
Thread Starter
Frenzied Member
Re: Download a file
Hi Mendhak
Thanks for the reply.
It works file when you click save on the dialog that appears however if you click open the application required is started e.g. Powerpoint or Acrobat Reader but an error is thrown saying the file cannot be found. The file is downloaded to the temporary files but then seems to disappear before the application can open it.
Any ideas who to get around this?
Dave
If I have been helpful please rate my post. If I haven't tell me!
-
May 25th, 2006, 05:21 AM
#4
Re: Download a file
I think I know why that's happening. The applications are opening the stream, which isn't the type they're expecting. Adobe Acrobat might be expecting application/pdf but is getting an octet stream.
What you need to do is to look at the extension of the file and appropriately change the Response.ContentType value. I'm assuming you already know the ContentTypes you should be using.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|