我正在通過我的web應用程序提供docx文件。這是爲了響應正在更改的DropDownList索引而完成的,我將AutoPostBack設置爲true。我有許多選項可以通過複選框進行選擇,並且在更改下拉列表時,創建的文件基於選中的選項。Asp.net - 通過Response.OutputStream服務文件後會發生什麼
所以我在做什麼是事件觸發時我做了一些魔法來創建一個文檔,然後爲它服務:
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()
現在的問題是,我應該在這一點上做什麼?看起來我必須調用Response.End()發送緩衝的內容,但這讓我坐在一個頁面上,複選框已勾選,而我的下拉列表已更改選項。如果我點擊頁面上的任何其他按鈕導致回發,似乎選定的索引更改了我的下拉菜單事件。
我想要做的是發送文件後重定向。由於Response.End()停止頁面執行,因此Response.redirect不起作用。誰能告訴我這種情況通常如何處理?任何人都可以告訴我,爲什麼我的下拉列表中選定的索引事件會再次觸發,如果頁面上的另一個控件在文件傳遞完成後進行回發?任何建議真的很感激。謝謝!
看看這個鏈接:http://stackoverflow.com/questions/3676734/redirecting-to-another-page-after-response-end-has-been-called-in-c-sharp – w0051977 2012-07-25 18:14:18
嗨,感謝您的迴應,但該主題中的答案對我無效。設置刷新似乎沒有做任何事情。我仍然得到了這個文件,但是之後它不會改變 – TheMethod 2012-07-26 11:19:33