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?
scm0smlAsked:
Who is Participating?
 
scm0smlConnect With a Mentor Author Commented:
Thanks for trying to help.

In the end I found this as a solution that I managed to get working:
http://geekswithblogs.net/GruffCode/archive/2010/10/28/detecting-the-file-download-dialog-in-the-browser.aspx
0
 
Dave BaldwinFixer of ProblemsCommented:
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

0
 
scm0smlAuthor Commented:
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....?!
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Dave BaldwinFixer of ProblemsCommented:
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.
0
 
scm0smlAuthor Commented:
If you could take a look at my previous question and see the response I was getting back when trying:
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_27859064.html

Any ideas?
0
 
Dave BaldwinFixer of ProblemsCommented:
That's showing the content of the PDF.  You already knew that.  It is not the HTTP repsonse header that you're seeing.
0
 
scm0smlAuthor Commented:
So how can I go about calling a javascript function then... I'm lost?
0
 
Dave BaldwinFixer of ProblemsCommented:
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.
0
 
scm0smlAuthor Commented:
Thats what I have at the moment.

The spinner comes on fine.

Its getting the thing to turn off again is causing the problem?
0
 
scm0smlAuthor Commented:
i'm just getting the open/save option for the pdf but nothing else is happening.

The spinner stays.
0
 
Dave BaldwinFixer of ProblemsCommented:
Ok, that looks good.
0
 
scm0smlAuthor Commented:
answered myself in the end
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.