Link to home
Start Free TrialLog in
Avatar of scm0sml
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:
 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();

Open in new window


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?
Avatar of Dave Baldwin
Dave Baldwin
Flag of United States of America image

What do you mean by "2 responses"?  A link to download your PDF will normally be passed to the designated application and most of us have the Adobe Reader plugin.

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:

http://www.dibsplace.com/webdev/WebSiteDesignOutline.pdf

GET /webdev/WebSiteDesignOutline.pdf HTTP/1.1
Host: www.dibsplace.com
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:15.0) Gecko/20100101 Firefox/15.0.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://www.dibsplace.com/webdev/index.html
Cookie: xxxx

HTTP/1.1 200 OK
Date: Tue, 11 Sep 2012 08:01:46 GMT
Server: Apache
Last-Modified: Mon, 19 Oct 2009 16:42:00 GMT
Etag: "3280985-3598f-4764c6cb16600"
Accept-Ranges: bytes
Content-Length: 219535
Keep-Alive: timeout=15, max=99
Connection: Keep-Alive
Content-Type: application/pdf

Open in new window

Avatar of scm0sml
scm0sml

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....?!
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.
Avatar of scm0sml

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?
That's showing the content of the PDF.  You already knew that.  It is not the HTTP repsonse header that you're seeing.
Avatar of scm0sml

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>

Open in new window

If the display is on the same page, it will disappear when the linked page, the PDF, loads.
Avatar of scm0sml

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?
Avatar of scm0sml

ASKER

i'm just getting the open/save option for the pdf but nothing else is happening.

The spinner stays.
ASKER CERTIFIED SOLUTION
Avatar of scm0sml
scm0sml

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, that looks good.
Avatar of scm0sml

ASKER

answered myself in the end