asp.net response dual content type - asp.net

i am sending a large amount of excel content data to the browser using Page.Response.Write("") in a for loop.
before starting the loop i am changing the conetnt type of the reponse.context property to "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. before the write i change back the content type to be text/html.
but i get an error that say that is not possible to change the content type after sending the HTTP Headers.
How to notify the browser about an error that occured during that for loop?
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.

Did you try 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.
Loop trough the content and add to a StringBuilder
If no error, set contenttype to ms-excel and write out SB
If there is an error, leave contenttype as it is write out your error message as needed.

Page.Response.Write("") will send data to the browser. 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. 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.
/Tibi

Related

Qt XmlHttpRequest inappropriately modifies Content-type request header value by itself

I am trying to upload a file to Dropbox using XmlHttpRequest, in a QML application. I am getting the following error:
Error in call to API function "files/upload": Bad HTTP "Content-Type"
header: "application/octet-stream;charset=UTF-8". Expecting one of
"application/octet-stream", "text/plain; charset=dropbox-cors-hack".
I seems that no matter which value I set to the Content-Type request header, it always adds
;charset=UTF-8
to the end.
My test data is simply a string, of which I would like a text file be generated to the user's dropbox folder.
request.send("TestString abcdefg")
How to deal with this issue?
Edit, with regards to the duplicate suspicion:
This is a similar issue, however, this particular case, if it can be addressed at all, needs to be addressed within the capabilities of Qt, on the QML javascript side, which has a set of different capabilities than javascript programming for the web, as it is a custom js engine, as far as I understand.

"Save As" popup in response to a POST request?

Is it possible to get the browser to popup its Save As... dialog when doing a POST (rather than GET) request?
Using the Spring framework, I'm trying to build a service that will receive some data (a two-dimensional json array), and produce an Excel file that the user is prompted to download.
By doing a GET request, eg by browsing directly to my URL, I can get the browser to display the popup by setting headers similar to this:
response.setHeader("Content-Disposition", "attachment; filename=" + fileName)
response.setContentType("application/vnd.ms-excel")
I need to allow the client to post the data that will be used to build the Excel file though, which implies a POST request. The same headers are returned, but no popup.
Is there a way to achieve this, or does the popup appear only for GET requests?
I'm thinking I could do a two-step process; 1) allow the client to POST the data, and return some sort of reference key, 2) allow the client to do a GET request and include the key, and return the relevant headers to cause the browser to popup the dialog.
Any other thoughts on how to do this?
Thanks
It's possible. I'm not sure what I did wrong the first time I tried it out.

How to add and access Custom Header in C#

I need to add a custom header something like
MYName: Balaji
which i need to access from .aspx file through
Request.ServerVariables["HTTP_MYName"];
should return "Balaji". I need so many variables like this it will added dynamically.
Kindly help.
Also, I cannot persist this varaibles in any of the .Net controls or objects like
cookies, sessions, application, hidden variable etc., or cannot store this in d/b and get it back whenever is required, I NEED IT ONLY IN HTTP HEADERS.
Kindly send the C# code how to add this variable and get the value back in .aspx file.
What do you mean by "get the value back in .aspx file"? HTTP headers are intended to be used as directives to a browser, how to interpret the given content. You don't have access to these values in your document.
Setting a custom HTTP header is quite easy, however:
Page.Response.AddHeader("MyCustomHeader", "VerySecretValue")
Updated my answer as per your comment.
If you need to transfer information between a HTTPModule and an ASPX page, you can use HTTPContext.Current, since this stays the same in both places.
So, you add it by
HttpContext.Current.Items.Add("SecretKey", "SecretValue");
and read it as
string s = HttpContext.Current.Items["SecretKey"];

Actionscript error

I have a flex application which is configured with blazeds sever and i am pulling images from remote database from my flex application, but as soon as i click the get image button to pull images it is giving some action script error.
TypeError: Error #2007: Parameter bytes must be non-null.
at flash.display::Loader/_loadBytes()
at flash.display::Loader/loadBytes()
I tried googling but couldn't found any solution.
Any help is appreciated.
You have a null parameter inside the function flash.display::Loader/loadBytes(). ;)
Short answer is that you're probably fetching the wrong url. Use firebug or Fiddler or something to determine whether the url is correct. If that's not the case, maybe you're not handling the right events.
Like the comments say, we need the code.
Thank You everyone it was a my code error, it give this error if no value is returned to load bytes . I was pulling a image from a table where no image was present and therefore returning null value in load bytes.

how to get the content of a value posted as the body in asp classic?

I've seen a couple of rest examples where the xml message gets posted in the body of the http request, and not in a named parameter...
in classic asp I have the request.form object that allows me to get the posted values, but I have to specify the name of the parameter...
is there some way to get the whole content of the post?
I would need the equivalent of
request.serverVariables("QUERY_STRING"), but for the post, not the get part of the http request...
( http://www.w3schools.com/ASP/coll_servervariables.asp )
would I have to use request.binaryRead()???
thanks a lot
ps: in java, I cold achieve this using request.getReader()...
how to get a the value of an http post as a whole? parsing restful post
--
just to clarify thing a little bit
when I say post a value as the body, I mean that the content of the message is not enconded like param1=value1&param2=value2...paramx=valuex
the message is the body itself... you can achieve this with any ajax library (like prototype) to test ir I'm using a firefox plugin that let you do that, it's named POSTER
https://addons.mozilla.org/en-US/firefox/addon/2691
A developer tool for interacting with web services and other web resources that lets you make HTTP requests, set the entity body, and content type. This allows you to interact with web services and inspect the results...
You didn't specify either what actual content type is being posted nor what you intented to do with it once you've acquired it.
Lets assume for a moment that the content is XML and you want to load it into an XML DOM.
A useful fact about the Request object is that it implements IStream where the stream is the entity body of the POST. Another useful fact is MSXML DOMDocument load method can accept an implementation of IStream. Hence:-
Dim xml: Set xml = CreateObject("MSXML2.DOCDocument.3.0")
xml.async = false
xml.load Request
This code loads the posted entity body into the DOM.
I think I found it
if you issue str( request.form ) you get the raw value of the form element...
works also with request.querystring and request.cookies
it doesn't work with request.serverVariables, throws an exception...
oh, and inspecting the debugger I've also found a completely undocumented property, request.body, that seems to behave just like the request.form property...
Are you trying to loop through all the posted values from the form? If so in ASP.OLD you could do this:
For Each Field in Request.Form

Resources