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?
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.
Re: setting content disposition
Quote:
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.
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.
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.
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.
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
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.
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.
Re: setting content disposition
I modified the code to add response.writefile and it works fine now
Thanks for your help
Re: setting content disposition *Resolved
is there a way to do this in a static html page? I dont have access to IIS
Re: setting content disposition
You'll need to do it through code or by setting a MIME type in IIS.