|
-
Sep 13th, 2006, 04:19 AM
#1
Thread Starter
Fanatic Member
setting content disposition
I have set up a page with a link to a file to download, that will prompt user to open, save or cancel rather than open the xls in browser like this:
VB Code:
Dim VirtualPath As String = Request.ServerVariables("SERVER_NAME").ToString
Dim file As String = Request.QueryString("href")
Dim filepath As String = "http://" & VirtualPath & file
Dim filename As String = System.IO.Path.GetFileName(filepath)
Response.ContentType = "application/x-msdownload"
lblHref.Text = ("<a href=" & filename & ">download</a>")
is there a way to open the save as box straight away without clicking save?
Last edited by davebat; Oct 17th, 2006 at 07:33 AM.
-
Sep 13th, 2006, 04:23 AM
#2
Re: setting content disposition
No, that is not possible since it's a browser-specific feature. The user is always prompted with the Run/SaveAs/Cancel dialog box.
-
Sep 13th, 2006, 04:38 AM
#3
Re: setting content disposition
Response.ContentType = "application/x-msdownload"
This is wrong, it should be application/octet-stream.
Although, really, if you want to force a download you should serve the .xls file from a different aspx file and set the Content-disposition header to attachment, as you suggested in the thread title. This cannot be set through a link.
After that point, as mend said, it's up to the user agent to decide what to do.
-
Sep 13th, 2006, 04:44 AM
#4
Thread Starter
Fanatic Member
Re: setting content disposition
Thanks guys, Ive changed it to
VB Code:
Dim VirtualPath As String = Request.ServerVariables("SERVER_NAME").ToString
Dim file As String = Request.QueryString("href")
Dim filepath As String = "http://" & VirtualPath & file
Dim filename As String = System.IO.Path.GetFileName(filepath)
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment; filename=""" & filename & """")
Response.Flush()
Response.Redirect("original url")
Only problem is after the file download prompt I would like it to redirect back to the page it came from, but nothing after the response.addheader gets processed. Do you know how I would do this.
-
Sep 13th, 2006, 04:51 AM
#5
Re: setting content disposition
You can't redirect after Flush(), the headers must be sent beforehand. That's why it has to be a separate script. If you link to that from the actual page, the browser should just show the download box without navigating away from the page.
-
Sep 13th, 2006, 04:54 AM
#6
Thread Starter
Fanatic Member
Re: setting content disposition
thanks, the only problem is this file has to be available for certain users through the asp.net roles, and there are other files on the page available to everyone. Now that I understand http headers a little better I'll be able to figure out a solution.
Thanks.
-
Sep 14th, 2006, 03:01 AM
#7
Thread Starter
Fanatic Member
Re: setting content disposition
I think im nearly there, the only thing is the file that is downloading has the right name, but is the actual aspx. net html rather than the spreadsheet>
VB Code:
Dim VirtualPath As String = Request.ServerVariables("SERVER_NAME").ToString
Dim file As String = Request.QueryString("href")
Dim filepath As String = "http://" & VirtualPath & file
Dim filename As String = System.IO.Path.GetFileName(filepath)
Dim fullpath As String = filepath & filename
Response.Clear()
If User.IsInRole("PRN222") Then
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-Disposition", "attachment; filename=" & filename)
Response.Flush()
Else
lblHref.Text = ("You do not have access to this report.")
End If
Last edited by davebat; Sep 14th, 2006 at 03:09 AM.
-
Sep 14th, 2006, 03:09 AM
#8
Re: setting content disposition
That's because you have't made it a separate script and loaded the spreadsheet file into the response stream. So all it is doing is flushing the current page.
Like I said, you need two .aspx files:
- One to serve the HTML
- One to serve the file download
Link to the download page from the HTML page. Do the headers/response magic in the download page.
-
Sep 14th, 2006, 03:14 AM
#9
Thread Starter
Fanatic Member
Re: setting content disposition
Thanks penagate, at the moment I have a aspx page with a list of links, when the link for this spreadsheet is pressed it goes to a download aspx page which has the above code. The download page is where I am trying to do the headers and response code.
-
Sep 14th, 2006, 03:24 AM
#10
Thread Starter
Fanatic Member
Re: setting content disposition
I modified the code to add response.writefile and it works fine now
Thanks for your help
-
Oct 17th, 2006, 07:32 AM
#11
Thread Starter
Fanatic Member
Re: setting content disposition *Resolved
is there a way to do this in a static html page? I dont have access to IIS
-
Oct 18th, 2006, 07:11 AM
#12
Re: setting content disposition
You'll need to do it through code or by setting a MIME type in IIS.
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
|