scm0sml
asked on
understanding custom httpresponse
Hi,
I have a page that exports a pdf (I'm using c#).
I have a button click and on that button click I am amongst other things doing the following:
I'm trying to get my head around having 2 responses back for the page.
How is that handled by the browser as it is from the same request? Or does this custom request override the first request.
My reason for asking is I somehow need to call I javascript function in this request as well.
Any ideas?
I have a page that exports a pdf (I'm using c#).
I have a button click and on that button click I am amongst other things doing the following:
var response = System.Web.HttpContext.Current.Response;
response.Clear();
response.AddHeader("Content-Type", "binary/octet-stream");
response.AddHeader("Content-Disposition", "attachment; filename=PMSQuestionnaireResults.pdf; size=" + pdfBytes.Length.ToString());
response.Flush();
response.BinaryWrite(pdfBytes);
response.Flush();
response.End();
I'm trying to get my head around having 2 responses back for the page.
How is that handled by the browser as it is from the same request? Or does this custom request override the first request.
My reason for asking is I somehow need to call I javascript function in this request as well.
Any ideas?
ASKER
Well I thought that I would get a response from the actual request ie my c# page? and then send this 2nd response.
That may be total rubbish :) ?
I tried with ajax but it didn't work, apparently you can't download files using jquery...
This is doing my head in as is such a simple thing I want to do.
Basically I have started a spinner using javascript on the button click, and somehow need to call a function that turns the spinner off....?!
That may be total rubbish :) ?
I tried with ajax but it didn't work, apparently you can't download files using jquery...
This is doing my head in as is such a simple thing I want to do.
Basically I have started a spinner using javascript on the button click, and somehow need to call a function that turns the spinner off....?!
AJAX just returns data to the javascript that asked for it and that isn't a download. I don't think the part of the browser that would recognize it as a download operates during AJAX.
In my post above, the section that starts with GET is the request header sent from the browser to the server. It would be the same if it was caused by clicking on a link or requested thru AJAX. The part starting with HTTP is the response header. Although the details vary for each different request, this is what always happens, this Is HTTP. A request is made and a response is given.
Your code may do any number of things on the server between the request and the response but these are the parts that the browser sees.
In my post above, the section that starts with GET is the request header sent from the browser to the server. It would be the same if it was caused by clicking on a link or requested thru AJAX. The part starting with HTTP is the response header. Although the details vary for each different request, this is what always happens, this Is HTTP. A request is made and a response is given.
Your code may do any number of things on the server between the request and the response but these are the parts that the browser sees.
ASKER
If you could take a look at my previous question and see the response I was getting back when trying:
https://www.experts-exchange.com/questions/27859064/httpresponse-with-jquery-and-c.html
Any ideas?
https://www.experts-exchange.com/questions/27859064/httpresponse-with-jquery-and-c.html
Any ideas?
That's showing the content of the PDF. You already knew that. It is not the HTTP repsonse header that you're seeing.
ASKER
So how can I go about calling a javascript function then... I'm lost?
You would have to put it in an 'onclick' statement in the link.
<a href="Your.PDF" onclick="yourjs();">Your PDF File</a>
If the display is on the same page, it will disappear when the linked page, the PDF, loads.
ASKER
Thats what I have at the moment.
The spinner comes on fine.
Its getting the thing to turn off again is causing the problem?
The spinner comes on fine.
Its getting the thing to turn off again is causing the problem?
ASKER
i'm just getting the open/save option for the pdf but nothing else is happening.
The spinner stays.
The spinner stays.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Ok, that looks good.
ASKER
answered myself in the end
You can use AJAX to make a HTTP Request but I don't know what will happen if you request a PDF file. AJAX is normally used to get something to display on the page.
This is what a request and response header for a PDF looks like:
Open in new window