Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

understanding custom httpresponse

Posted on 2012-09-11
12
Medium Priority
?
275 Views
Last Modified: 2012-10-01
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?
0
Comment
Question by:scm0sml
  • 7
  • 5
12 Comments
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38385841
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
 

Author Comment

by:scm0sml
ID: 38385848
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
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38385884
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:scm0sml
ID: 38385892
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
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38385898
That's showing the content of the PDF.  You already knew that.  It is not the HTTP repsonse header that you're seeing.
0
 

Author Comment

by:scm0sml
ID: 38385906
So how can I go about calling a javascript function then... I'm lost?
0
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38385916
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
 

Author Comment

by:scm0sml
ID: 38385923
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
 

Author Comment

by:scm0sml
ID: 38385949
i'm just getting the open/save option for the pdf but nothing else is happening.

The spinner stays.
0
 

Accepted Solution

by:
scm0sml earned 0 total points
ID: 38386157
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
 
LVL 84

Expert Comment

by:Dave Baldwin
ID: 38387794
Ok, that looks good.
0
 

Author Closing Comment

by:scm0sml
ID: 38450077
answered myself in the end
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Screencast - Getting to Know the Pipeline
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month21 days, 7 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question