I've been pulling my hair out for hours trying to figure this out.
I have an iframe with which I want to download a file. It all works fine, but the iframe onload is not called if my Response.ContentType = "APPLICATION/OCTET-STREAM";
My javascript function is as follows:
function DownloadStuff(){
var DownloadSource = "http://Apage.aspx"
var iframe = $("#hiddenDownloader");
if (iframe.attr("id") === undefined) {
$('<iframe />', { id: 'hiddenDownloader', onload:'javascript:alertReady();' }).appendTo('body');
iframe = $("#hiddenDownloader");
}
iframe.attr('src', DownloadSource);
}
function alertReady() {
alert("Ready");
}
My server side code is as follows:
Response.Clear();
Response.CacheControl = "no-cache";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetValidUntilExpires(false);
Response.ExpiresAbsolute = DateTime.Parse("1/1/2000");
Response.Expires = 0;
Response.ContentType = "APPLICATION/OCTET-STREAM";
Response.AddHeader("Content-Disposition", "attachment;filename=\"ReportDeck.pptx\"");
byte[] bytes = (byte[])PowerPointFile.Tables[0].Rows[0]["PPTBlob"];
bytes = Compression.DeCompressByteArray(bytes);
Response.OutputStream.Write(bytes, 0, bytes.Length);
If I remove the ContentType and Header the onload is called, but then the file is not downloaded through the save file dialog, but is instead written into the iframe.
Any help is appreciated.
Regards,
Byron Cobb.
Well yeah. onload is only called when a document is loaded into an iframe. A Save operation does not load a document into the frame; the old document is left in place and the save-as dialogue is popped-up.
You cannot detect that a file download has been accepted or completed from JavaScript alone. If you really need to detect that, the closest you would be able to get would be to have to have the server-side store the progress of the download by a unique ID, and provide an AJAX interface for the client-side script to query whether the server had finished sending the file yet.
Incidentally:
onload:'javascript:alertReady();'
is doubtful. You don't need and shouldn't use javascript: in event handler attributes (that's only for href="javascript:..." URLs, which should also never be used!). And setting an event handler attribute to a string from JS is highly dodgy. Passing onload: alertReady directly would be better, or since you're using jQuery use the standard binding methods like iframe.load(function() { ... });.
Related
I have a page that exports large amount of data to excel 2007 macro enabled format. I have Response.Redirect at the end to redirect to another page after the excel file is created but the redirect does not work when I have really large amount of data i.e about 60,000+ rows.
Below is the snippet of the code that I am using to save the excel file:
Response.Clear();
Response.AppendHeader("content-disposition", "attachment; filename=RegionData.xlsm");
Response.ContentType = "application/octet-stream";
wbRegionData.Save(Response.OutputStream);//();
Response.End();
Response.Redirect("RegionData.aspx?PALID=" + AVListID, true);
Thank you for your help.
You cannot do a file download and a redirect in the same server response. If you need to perform a redirect after a successful file download you will need to somehow tell the client that a file download successfully occurred which can only be accomplished by writing a cookie as far as I know.
I have created the jQuery File Download plugin (Demo) (GitHub) (Blog Posts) that simplifies a lot of this stuff, using the plugin your code could be like this:
ASP.NET CODE:
Response.Clear();
Response.AppendHeader("content-disposition", "attachment; filename=RegionData.xlsm");
Response.ContentType = "application/octet-stream";
Response.SetCookie(new HttpCookie("fileDownload", "true"){Path = "/"});
wbRegionData.Save(Response.OutputStream);//();
Response.End();
JavaScript:
$.fileDownload("/urltofiledownload/", {
successCallback: function (url) {
window.location = "/redirecturl/";
}
});
Without more of your code I can't give you a complete solution (look at the Demo for examples) but I think using the plugin would be your easiest and best bet.
You can't because you are closing the response by calling Response.End(); You must have to add define these two actions separately (Download and redirection).
It will not work because the Respose.End() Method stop the normal execution of the page, I have refatored your code and delete de Response.End() Method, now it's work.
Response.Clear();
Response.AppendHeader("content-disposition", "attachment; filename=RegionData.xlsm");
Response.ContentType = "application/octet-stream";
wbRegionData.Save(Response.OutputStream);//();
Response.Redirect("RegionData.aspx?PALID=" + AVListID, true);
I need to run several methods after sending file to a user for a download. What happens is that after I send a file to a user, response is aborted and I can no longer do anything after response.end().
for example, this is my sample code:
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=test.pdf");
Response.ContentType = "application/pdf";
byte[] a = System.Text.Encoding.UTF8.GetBytes("test");
Response.BinaryWrite(a);
Response.End();
StartNextMethod();
Response.Redirect(URL);
So, in this example StartNextMethod and Response.Redirect are not executing.
What I tried is I created a separate handler(ashx) with the following code:
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
context.Response.AddHeader("content-disposition", "attachment; filename=test.pdf");
context.Response.ContentType = "application/pdf";
byte[] a = System.Text.Encoding.UTF8.GetBytes("test");
context.Response.BinaryWrite(a);
context.Response.End();
}
and call it like this:
Download d = new Download();
d.ProcessRequest(HttpContext.Current);
StartNextMethod();
Response.Redirect(URL);
but the same error happen. I've tryied to replace Response.End with CompleteRequest but it doesn't help.
I guess the problem is that I'm using HttpContext.Current but should use a separate response stream. Is that correct? how do I do that in a separate method generically (Assume that I want my handler to accept byte array of data and content type and be downloadable from a separate response. I really do not want to use a separate page for a response.
UPDATE
I still didn't find a good solution. I'd like to do some actions after user has downloaded a file, but without using a separate page for a response\request thing.
Update
Since you said no second page, do this instead. Add a section to your page that checks for a query string parameter (something like fileid, or path, etc...). If this value is present then it initiates the download process using your existing code. If this value is not present then it runs like normal.
Now when the user clicks the download link you perform a post back (which you are already doing). In this post back create an iFrame on the page and set the URL of the iFrame to your pages URL with the added query string parameter (mypage.aspx?id=12664 or ?download=true, something like that). After creating the iframe perform what ever additional databinds/etc... you wish too.
Example
- http://encosia.com/ajax-file-downloads-and-iframes/
This above linked example uses an iFrame and an update panel, just like you are talking about.
Original Post
Response.Flush will allow you to continue processing after you send the file to the user, or just don't call Response.End (you don't really need too).
However Daniel A. White is correct, you can't actually redirect from your code after you send a file, you will get an error if you try. BUT you can continue to perform other server side operations if you need to.
Other answers agree with the general consensus, you can't redirect after a file starts downloading: https://stackoverflow.com/a/822732/328968 (PHP, but same concepts since it involves HTTP in general). or Directing to a new page after downloading a file.
Response.End() throws a thread abort exception. It is designed to end your response.
No code after that will process in that thread.
The End method causes the Web server to stop processing the script and return the current result. The remaining contents of the file are not processed.
What is it that you are trying to achieve?
If your purpose it to allow the pdf to download and then take the user to some other page, a little javascript can help you out.
Add a script with a timer to set location.href to your redirected paged.
As the previous answers had stated - returning PDF file means to send HTTP headers. You cannot send another headers after that, and Response.Redirect() simply means to send HTTP 302.
If you don't want to have separate page, or if you don't want to use AJAX, why not trying:
<head>
<meta http-equiv="refresh" content="3; url=http://www.site.com/download.aspx?xxxx">
</head>
Actually this will show the desired page you want to show to the user, and will refresh the page after 3 sec with the URL for download of the PDF file.
Download the file in chunks, as illustrated File Download in ASP.NET and Tracking the Status of Success/Failure of Download or in the answer to this question. When the last chunk of the file has been written to the client you can execute the code you need to. (Doesn't have to be at the end, can be anywhere in between depending upon your needs.)
the user clicks on a download button on WebForm1.aspx to start downloading a file. then, after the file download is done (served by WebForm2.aspx), user is automatically redirected.
WebForm1.aspx
<script type="text/javascript">
$(document).ready(function () {
$('#btnDL').click(function () {
$('body').append('<iframe src="WebForm2.aspx" style="display:none;"></iframe>');
return true;
});
});
</script>
<asp:Button runat="server" ID="btnDL" ClientIDMode="Static" Text="Download" OnClick="btnDL_Click" />
WebForm1.aspx.cs
protected void btnDL_Click(object sender, EventArgs e)
{
var sent = Session["sent"];
while (Session["sent"]==null)
{// not sure if this is a bad idea or what but my cpu is NOT going nuts
}
StartNextMethod();
Response.Redirect(URL);
}
WebForm2.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=test.pdf");
Response.ContentType = "application/pdf";
byte[] a = System.Text.Encoding.UTF8.GetBytes("test");
Response.BinaryWrite(a);
Session["sent"] = true;
}
Global.asax.cs
protected void Session_Start(object sender, EventArgs e)
{
Session["init"] = 0; // init and allocate session data storage
}
note: make sure don't use ashx (generic handler) to serve your download. for some reason, the session in ashx and aspx don't talk to each other, unless you implement this.
Just remove the context.Response.End(); because you are redirecting anyway...
The problem is flawed logic here.... Why would you end the response?
Get the PDF and display a link to it or use a META refresh to redirect to the location of the PDF or you could also display a link or use a combination of both techniques.
I believe what you are trying won't work.
This is what I would do:
Write content to a file locally and assign it an unique id
send user to the next page that contains a hidden frame that perform a request with the unique id (javascript)
hidden request page loads file and push on the content stream.
This is the same behavior a lot of file download sites is using. Only issue is if the hidden frame fails (javascript turned off) to perform the request, why a lot of the same sites have the link available if the auto request fails.
Disadvantage: file cleanup.
I recommend this solution :
Don't use response.End();
Declare this global var : bool isFileDownLoad;
Just after your (Response.BinaryWrite(a);) set ==> isFileDownLoad = true;
Override your Render like :
///
/// AEG : Very important to handle the thread aborted exception
///
///
override protected void Render(HtmlTextWriter w)
{
if (!isFileDownLoad) base.Render(w);
}
I put together a download script after some wonderful help from stack overflow the other day. However I have now found that after the file has been downloaded I need to reload the page to get rid of the progress template on the aspx page. The code to remove the template worked before I added in the download code.
Code to remove progress template: upFinanceMasterScreen.Update();
I've tried calling putting this before and after the redirect to the IHttpHandler
Response.Redirect("Download.ashx?ReportName=" + "RequestingTPNLeagueTable.pdf");
public class Download : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
StringBuilder sbSavePath = new StringBuilder();
sbSavePath.Append(DateTime.Now.Day);
sbSavePath.Append("-");
sbSavePath.Append(DateTime.Now.Month);
sbSavePath.Append("-");
sbSavePath.Append(DateTime.Now.Year);
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ContentType = "application/pdf";
HttpResponse objResponce = context.Response;
String test = HttpContext.Current.Request.QueryString["ReportName"];
HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=" + test);
objResponce.WriteFile(context.Server.MapPath(#"Reports\" + sbSavePath + #"\" + test));
}
public bool IsReusable { get { return true; } }
Thanks for any help you can provide!
When you send back a file for the user to download, that is the HTTP request. In other words, you can either have a post-back which refreshes the browser page or you can send a file for the user to download. You cannot do both without special tricks.
This is why most sites when you download a file, it first takes you to a new page that says, "Your download is about to begin", and then subsequently "redirects" you to the file to download using meta-refresh or javascript.
For example, when you go here to download the .NET 4 runtime:
http://www.microsoft.com/downloads/en/confirmation.aspx?FamilyID=0a391abd-25c1-4fc0-919f-b21f31ab88b7&displaylang=en&pf=true
It renders the page, then uses the following meta-refresh tag to actually give the user the file to download:
<META HTTP-EQUIV="refresh" content=".1; URL=http://download.microsoft.com/download/9/5/A/95A9616B-7A37-4AF6-BC36-D6EA96C8DAAE/dotNetFx40_Full_x86_x64.exe" />
You'll probably have to do something similar in your app. However, if you are truly interested in doing something after the file is completely downloaded, you're out of luck, as there's no event to communicate that to the browser. The only way to do that is an AJAX upload like gmail uses when you upload an attachment.
In my case, I was using MVC and I just wanted the page to refresh a few seconds after the download button was selected in order to show the new download count. I was returning the file from the controller.
To do this I simply changed the view by adding an onclick event to the download button that called the following script (also in the view):
setTimeout(function () {
window.location.reload(1);
}, 5000);
It fit my purpose... hope it helps someone else.
This is quick and easy to hack if needed.
Step 1: Add hidden button to .aspx page:
<asp:Button ID="btnExportUploaded" runat="server" Text="Button" style="visibility:hidden" OnClick="btnExportUploaded_Click" CssClass="btnExportUploaded" />
Step 2: Perform your default postback action and at the end register a startup script with jquery call which will trigger the hidden button click and cause a file to download:
ClientScriptManager cs = Page.ClientScript;
cs.RegisterStartupScript(this.GetType(), "modalstuff", "$('.btnExportUploaded').click();", true);
A simpler approach is to just do whatever needed in the PostBack event, and register a reload script with an additional argument to indicate the download.
Something like:
C# code:
protected void SaveDownloadCount(int downloadId)
{
// Run in a PostBack event.
// 1) Register download count, refresh page, etc.
// 2) Register a script to reload the page with an additional parameter to indicate the download.
Page.ClientScript.RegisterStartupScript(GetType(), "download",
"$(document).ready(function(){window.location.href = window.location.pathname + window.location.search ? '&' : '?' + 'printId={0}';});".Replace("{0}", downloadId.ToString()), true);
}
Then, in PageLoad we need to check for the download paramenter and serve the file:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int printId;
if (Request.QueryString["printId"] != null && int.TryParse(Request.QueryString["printId"], out printId))
{
// Check if the argument is valid and serve the file.
}
else
{
// Regular initialization
}
}
}
This is simalar to #puddleglum answer but without the drawback of the "out of synch" timeout.
I'm downloading text from a website and I want to have the user be able to save it as a file. So, I have the following code that does just that.
protected void DownloadFile(string fileName, string content)
{
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.ContentType = "text/plain";
Response.Write(content);
Response.End();
}
The problem I am having is that I get an exception after this code runs. I believe it's due to the fact that I call Response.End(). So, every time a user downloads a file it redirects them to the generic error page because all generic exceptions redirect to that page.
Any ideas on how I can write text out to a file and not get this error? If I remove Response.End() I get my text and then the rest of the HttpResponse text, but I don't get the error.
Thanks.
First, if this code is inside a .ASPX file, you need to move it out to a .ASHX file.
Second, after you've moved to .ASHX you can simply write to the output stream and be done, you shouldn't need a Response.End();
This is the link I started with: http://dotnetperls.com/ashx-handler -- there isn't much to it. It simply removes a bunch of the asp.net "page" overhead.
Ideally you should have a page with a list, and a link/button to your "download.ashx" file. Then pass it a record id on the query string so it can do the lookup and response.write calls.
Don't call Response.End(). There is still code to be run server-side after you have populated the headers whether you intend to or not. Instead consider using Response.Flush() and allowing the page to complete. Another alternative would be to use a HttpHandler (ashx) file instead of an aspx/ascx file. This will give you more basic access to the Response so you should be able to end without repercussion.
Maybe you want to call Response.Output.Write() instead?
This code will always make my aspx page load twice. And this has nothing to do with AutoEventWireup.
Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "inline;filename=data.pdf");
Response.BufferOutput = true;
byte[] response = GetDocument(doclocation);
Response.AddHeader("Content-Length", response.Length.ToString());
Response.BinaryWrite(response);
Response.End();
This code will only make my page load once (as it should) when I hardcode some dummy values.
Response.Clear();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "inline;filename=data.pdf");
Response.BufferOutput = true;
byte[] response = new byte[] {10,11,12,13};
Response.AddHeader("Content-Length", response.Length.ToString());
Response.BinaryWrite(response);
Response.End();
I have also increased the request length for good measure in the web.config file.
<httpRuntime executionTimeout="180" maxRequestLength="400000"/>
Still nothing. Anyone see something I don't?
GetDocument(doclocation);
May be this method somehow returns Redirection code ? or may be an iframe or img for your dynamic content?
If so:
In general the control could get called twice because of the url response. First it renders the content. After that your browser tries to download the tag (iframe,img) source which is actually a dynamic content that is generated. So it makes another request to the web server. In that case another page object created which has a different viewstate, because it is a different Request.
Have you found a resolution to this yet? I having the same issue, my code is pretty much a mirror of yours. Main difference is my pdf is hosted in an IFrame.
So interesting clues I have found:
If I stream back a Word.doc it only gets loaded once, if pdf it gets loaded twice. Also, I have seen different behavior from different client desktops. I am thinking that Adobe version may have something to do with it.
Update:
In my case I was setting the HttpCacheability to NoCache. In verifying this, any of the non client cache options would cause the double download of the pdf. Only not setting it at all (defaults to Private) or explicitly setting it to Private or Public would fix the issue, all other settings duplicated the double load of the document.
Quick Guess: Could it be that at this stage in the page life cycle, the class that contains GetDocument() has already gone through garbage collection? The ASP.NET Worker process then needs to reload the page in order to read that method again?
Have you tried it in the Page_Load ? and why is GetDocument a static method?