Is it possible to write more than one content type to a Page's response stream in ASP.NET?
I'm working on a web part in a Sharepoint application which first writes some HTML to the page response stream in order to close a modal dialog and then writes a byte array for a Word document as an attachment to be opened in the browser.
The following code illustrates what I would like to do:
var response = Page.Response;
response.ContentType = "text/html";
response.Write(
"<script type=\"text/javascript\">//javascript</script>");
response.Flush();
response.ContentType = "application/octet-stream";
response.AddHeader("Content-Disposition",
"attachment; filename=WordFile.docx");
response.Bi开发者_开发百科naryWrite(byteArray);
response.End();
This code gives an exception with the message:
Server cannot set content type after HTTP headers have been sent
Which makes sense for this code. Is there any way I can work around this?
No, there isn't, given that the content type is sent in the HTTP response, and that you are sending one response, the content type can only be one type.
That said, you will have to have your client issue two separate requests, one to get the HTML content, then once to get the Word document.
精彩评论