Results 1 to 12 of 12

Thread: setting content disposition

  1. #1

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727

    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:
    1. Dim VirtualPath As String = Request.ServerVariables("SERVER_NAME").ToString
    2. Dim file As String = Request.QueryString("href")
    3. Dim filepath As String = "http://" & VirtualPath & file
    4. Dim filename As String = System.IO.Path.GetFileName(filepath)
    5. Response.ContentType = "application/x-msdownload"
    6.  
    7. 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.

  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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.

  3. #3
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  4. #4

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727

    Re: setting content disposition

    Thanks guys, Ive changed it to

    VB Code:
    1. Dim VirtualPath As String = Request.ServerVariables("SERVER_NAME").ToString
    2.         Dim file As String = Request.QueryString("href")
    3.         Dim filepath As String = "http://" & VirtualPath & file
    4.         Dim filename As String = System.IO.Path.GetFileName(filepath)
    5.  
    6.         Response.ContentType = "application/octet-stream"
    7.  
    8.         Response.AddHeader("Content-Disposition", "attachment; filename=""" & filename & """")
    9.  
    10.         Response.Flush()
    11.    
    12.         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.

  5. #5
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  6. #6

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727

    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.

  7. #7

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727

    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:
    1. Dim VirtualPath As String = Request.ServerVariables("SERVER_NAME").ToString
    2.         Dim file As String = Request.QueryString("href")
    3.         Dim filepath As String = "http://" & VirtualPath & file
    4.         Dim filename As String = System.IO.Path.GetFileName(filepath)
    5.         Dim fullpath As String = filepath & filename
    6.  
    7.         Response.Clear()
    8.  
    9.         If User.IsInRole("PRN222") Then
    10.             Response.ContentType = "application/octet-stream"
    11.             Response.AddHeader("Content-Disposition", "attachment; filename=" & filename)
    12.             Response.Flush()
    13.                   Else
    14.             lblHref.Text = ("You do not have access to this report.")
    15.         End If
    Last edited by davebat; Sep 14th, 2006 at 03:09 AM.

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,647

    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.

  9. #9

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727

    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.

  10. #10

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727

    Re: setting content disposition

    I modified the code to add response.writefile and it works fine now

    Thanks for your help

  11. #11

    Thread Starter
    Fanatic Member davebat's Avatar
    Join Date
    Dec 2002
    Posts
    727

    Re: setting content disposition *Resolved

    is there a way to do this in a static html page? I dont have access to IIS

  12. #12
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    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
  •  



Click Here to Expand Forum to Full Width