Note that there are some explanatory texts on larger screens.

plurals
  1. POAsp.net - What do to after serving a file via Response.OutputStream
    primarykey
    data
    text
    <p>I am serving an docx file through my web application. This is done in response to a DropDownList index being changed, I have AutoPostBack set to true. I have a number of options that can be selected via checkboxes and when the dropdown is changed the file that is created is based on the checked options. </p> <p>So what I am doing is when the event fires I do some magic to create a document then serve it:</p> <pre><code>Protected Sub myDropDown_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles myDropDown.SelectedIndexChanged ' a bunch of things happen here but the end result is the contents of a docx file in a MemoryStream with the ' name mainStream 'here is where I set up the response and serve the file: Response.ClearContent() Response.ClearHeaders() Response.AddHeader("content-disposition", "attachment; filename=document.docx") Response.ContentEncoding = System.Text.Encoding.UTF8 Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document" mainStream.WriteTo(Response.OutputStream) mainStream.Close() mainStream.Dispose() Response.End() </code></pre> <p>Now my question is, what should I do at this point? It seems I have to call Response.End() send the buffered content but that leaves me sitting on a page with checkboxes checked and my dropdown on the changed option. If I hit any other buttons on the page that cause a postback it seems that the selected index changed event of my dropdown fires again. </p> <p>What I would like to do is redirect after sending the file. Response.redirect will not work as Response.End() stops page execution. Could anyone tell me how situations like this are typically handled? Could anyone tell me why the selected index event for my dropdown fires again if another control on the page does a postback after the file has been served? Any advice is really appreciated. Thanks!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload