Page rendering and http handlers - c#

We have a page that opens in a new browser window where there's an automatic redirect to an ASHX handler that produces some kind of an XLS file. It's done using a javascript redirect, i.e. setting window.location.href to the URL of the ASHX.
Although it works and presents the download dialog for the file, setting window.location.href also clears the content of the window so that it stays blank. It somehow makes sense but still it would be nice to keep the content of the previous page there while opening the download dialog in the foreground. Is it possible somehow (by defering the execution of the redirect or using a different technique to call the ASHX handler) ?
Another nice to have thing would be if we could close the parent page after the download dialog is presented, could this work in any way ?

Dynamically creating (in javascript) an hidden iframe that points to the download ashx location and adding it to the DOM would do the trick.

By using the content-disposition header in your response from your handler you can display the save dialog without having to open a new window. You won't need to use javascript to open a new window or create an iframe.
response.Clear();
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("content-disposition", "attachment; filename=sample.xls");
response.AddHeader("content-legth", content.Length.ToString());
response.Write(content.ToString());
response.End();
See this question for the possible excel mime types ( contentType )
Setting mime type for excel document

Related

iframe in IE going blank

I have a aspx page containing an iframe which shows a pdf ( i have ashx handler to display pdf in iframe), I also have a download button which onclick stream's a different pdf to the browser as an attachment.
download to browser code: (this is in ashx handler)
byte[] bytes = // get byte array from table
Response.Buffer = false;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
// [optional]
Response.AddHeader(
"Content-Disposition",
string.Format("attachment; filename={0}", attachmentName)
);
Response.OutputStream.Write(bytes, 0, bytes.Length);
Response.End();
This code works fine however after sending this response to the browser my iframe goes blank and I have to refresh the page in order to see it again. I tried most of the solutions
http://adamyoung.net/IE-Blocking-iFrame-Cookies
http://forums.asp.net/p/1036628/1433536.aspx
but none of them is working. iframe behaves fine in chrome & FF.
Note: if i do downloading stuff using asp:hyperlink to my ashx and setting target="_blank" it works fine in IE but i need some server side processing to do therefore i cant use this method.
While Chrome and FF leave the page there, this does actually make sense.
You are posting back to the server, then clearing your whole output, placing a file in the stream and sending that. Technically I would say IE is right in blanking out the page.
This is assuming your download button is in the iframe?
The solution I would try is putting that download link in another iFrame or have another page that downloads it and when you press a link it opens the new page which just outputs a file. So the window flashes up but then it disappears and a save dialog button appears. However popup blockers might try to stop this.

open dialog to download pdf

I have a gridview in which I have provided an option for the user to download the pdf files. When they click on the pdf icon sometimes it open the pdf file in a new tab and sometimes it starts downloading. How can i make it download always?
You need to add a button (image button, linknbutton or button) and handle the RowCommand event of GridView. In RowCommand handler you may write code to download a file.
You may use Response object's method.
string filepath=MapPath("~/files/file.pdf");
byte []bytes=System.IO.File.ReadAllBytes(filepath);
Response.Clear();
Response.ClearHeaders();
Response.AddHeader("Content-Type", "application/octet-stream");
Response.AddHeader("Content-Length", bytes.Length.ToString());
Response.AddHeader("Content-Disposition","attachment; filename=file.pdf");
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
In order to always force a download you need to add the Content-Disposition header as AVD showed; however, I find this totally unnecessary; I think it would suffice to have the link to the PDF open in a new window. In other words, have target="_blank" defined. Example:
invoice
Then, is up to the user whether he wants to save the file locally or just see it on the screen. I think the important thing is that this won't interfere with the current page the user is looking at.

Http Response Object in new window

I have a button click event set up to retrieve a byte array object from my DB and it is then going to show the file in a new browser window. Right now I have this much:
Response.ContentType = "image/jpeg";
Response.AddHeader("content-length", fileBytes.Length.ToString());
Response.BinaryWrite(fileBytes);
where fileByes is my byte array. This is working perfectly, but I need to force this to open in a new window. I have tried adding the javascript to the response with response.write but that doesn't seem to work.
Writing your response is handled server side. Displaying your response is handled client side. You would have to tell your client to open a new window given the response from the server, e.g.
Get Image
Where getImage.aspx is the ASP.NET page responsible for serving the image/page.
You can't open a new window from server-side code. You'll need to call window.open() from JavaScript and pass in a URL to a page that returns the file.
You'd want to have your button click open the new browser window, which then makes the call to your code you have posted in your question. You're trying to do it sort of backwards.
Use a hyperlink with a URL to a blank .aspx,
pass a parameters in the URL as ?param=4&param2 ... etc.
In the load event for the page place your response code there.

Download a file using AJAX

What's the best way to allow a user to pull down an RDP file, but to do so using AJAX? In other words, I have a hyperlink and I need an RDP file to be downloaded by the user, but without a full page refresh.
I tried to make an AJAX call using the following example for RDP. It seems to work on Chrome and Firefox, but not on any version of IE.
String content = <RDP Content Here>
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=HelloWorld.rdp");
Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
Response.ContentType = "Content-Type=application/x-rdp rdp;charset=ISO-8859-1";
Response.AddHeader("Content-Length", content.Length.ToString());
Response.Write(content);
Response.End();
I'd create another page and put that code there. Then have that page open up in a new window when the link is clicked. Make you clear out any of the boiler plate code that VS automatically generates when you create a new file. That'll get you your data without a page refresh. I've done this with Excel pages and images and it's worked like a champ.

ASP.Net Control Button PostBack is causing redirection, when I don't want it to at all (print button)

here's how my scenario is created.
I click a button which produces a pdf via iTextSharp.
I then click 'Cancel' on the pop-up dialog.
I navigate to another page via hyperlink.
I then click the 'Back' button on IE8
I then click the button which produces the PDF, then I'm redirected to the page I previously viewed (when I clicked the hyperlink). Which is not intended what so ever.
Here's some code that affects the Response object (code is within print_click event)
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment; filename=ManageWorkforce.pdf");
Response.End();
Anyone have a theory why this is happening?
I have always done this with a generic handler like Greg mentioned. This is also nicer if you want to link to the file from other pages in the future, or save it as a favorite, send it to your friend, etc.
Have you tried using
Response.Clear();
Response.ClearHeaders();
to clear any other content that may still be attached to the response content and headers?
Try to put:
<%# OutputCache NoStore="true" %>
on the aspx page.

Categories

Resources