简体   繁体   English

asp.net响应双重内容类型

[英]asp.net response dual content type

i am sending a large amount of excel content data to the browser using Page.Response.Write("") in a for loop. 我正在for循环中使用Page.Response.Write(“”)将大量的excel内容数据发送到浏览器。

before starting the loop i am changing the conetnt type of the reponse.context property to "ms-excel". 在开始循环之前,我将reponse.context属性的竞争类型更改为“ ms-excel”。

if i have an exception during that for loop , i try to popup an error message to the browser by registering an HTML startup script block and Write it to the browser. 如果在for循环中有异常,我会尝试通过注册HTML启动脚本块并将其写入浏览器来向浏览器弹出错误消息。 before the write i change back the content type to be text/html. 在写之前,我将内容类型改回为text / html。 but i get an error that say that is not possible to change the content type after sending the HTTP Headers. 但我收到一条错误消息,说发送HTTP标头后无法更改内容类型。

How to notify the browser about an error that occured during that for loop? 如何将for循环期间发生的错误通知浏览器? Is it possible to have more that one respone to the same browser tab? 是否可以在同一浏览器选项卡中进行多个响应? by the way,right now i don't want to use ASP.NET AJAX. 顺便说一句,现在我不想使用ASP.NET AJAX。

Did you try Response.ClearHeaders(); 您是否尝试过Response.ClearHeaders(); before changing the context type? 在更改上下文类型之前?

If that doesn't work because your Response.Write is causing problems, you might think about going the route of building a string before deciding your content type and then setting the headers. 如果由于Response.Write导致问题而无法解决问题,则可以考虑在确定内容类型然后设置标题之前采用构建字符串的方法。

  • Loop trough the content and add to a StringBuilder 循环浏览内容并添加到StringBuilder
  • If no error, set contenttype to ms-excel and write out SB 如果没有错误,请将contenttype设置为ms-excel并写出SB
  • If there is an error, leave contenttype as it is write out your error message as needed. 如果有错误,请保留contenttype,因为它是根据需要写出错误消息的。

Page.Response.Write("") will send data to the browser. Page.Response.Write(“”)会将数据发送到浏览器。 When you do that the browser needs to know the type of content it gets in order to render it correct. 当您执行此操作时,浏览器需要知道其获取的内容类型才能正确显示。 If you already told the browser what kind of content you have(and even if you don't, you are already sening data with Response.Write, it will use the default value which I think is text/html) then you can't take it back. 如果您已经告诉浏览器您拥有什么样的内容(即使您没有,即使您已经在使用Response.Write整理数据,它将使用我认为是text / html的默认值),那么您将无法把它收回。 Whatever you are saying to the client is said and you can't take it back. 无论您对客户说的是什么,您都无法收回。 The way to solve your problem is to cache the reply as suggested by Doozer. 解决问题的方法是按照Doozer的建议缓存答复。

/Tibi /提比

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在ASP.NET MVC中设置空响应的Content-Type - Setting the Content-Type of an empty response in ASP.NET MVC ASP.NET响应内容 - ASP.NET Response Content asp.net WebApi:未能序列化内容类型“application/xml”的响应正文; 字符集=utf-8' - asp.net WebApi: failed to serialize the response body for content type 'application/xml; charset=utf-8' 如何从ASP.NET Core MVC响应的Content-Type中删除字符集? - How do I remove the charset from Content-Type in a ASP.NET Core MVC response? 如何将API响应“内容类型为jpeg的文件流”转换为ASP.NET中的图像文件 - How to convert API response “File Stream with content type jpeg” to image file in ASP.NET ASP.NET Response.Filter:对整个内容/完整响应 - ASP.NET Response.Filter: on the entire content/full response 双重身份验证类型asp.net core 2项目中的AllowAnonymous操作始终为404状态代码 - Always 404 status code on AllowAnonymous action in dual authentication type asp.net core 2 project 在 ASP.NET 5 响应中设置 Content-Length 标头 - Setting Content-Length header in ASP.NET 5 response 用我自己的内容替换asp.net Response.Output - replace asp.net Response.Output with my own content 如何从asp.net中的用户控件中清除父响应内容? - How to clear a parent response content from a usercontrol in asp.net?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM