We have a Html.BeginForm(...) with a download button in it. When the button is clicked we enable a busy indicator prior to the submit. The form posts to the controller and the controller returns a FileStreamResult with the response headers like set so the file is downloaded instead of opened or begin redirected.
HttpContext.Response.AddHeader("content-disposition", "attachment; filename=downloaded-file.pdf");
// Return from the controller.
return new FileStreamResult(GetTestFile(), "application/pdf");
I need a way to now disable the busy indicator once the controller returns since there is no redirection.
One solution that i recommend is to detect when browser receives file download
It referenced this link and this link
The ideea is following: it's sends a cookie (C# Generated) with the file (that you are downloading). Use a window.setInterval to query for the presence of a cookie at regular intervals and check its value,and, if you have that cookie you can disable busy indicator.
Related
I'm trying to download a zip file (that is normally accessed/downloaded by pressing a button on a web page) using C#.
Normally the file is downloaded by selecting "Data Export" and then clicking the "SEARCH" button at this URL:
http://insynsok.fi.se/SearchPage.aspx?reporttype=0&culture=en-GB&fromdate=2016-05-30&tomdate=2016-06-03
If trigger the download manually on the webpage and then copy the download url from the 'Downloads' view of chrome or firefox I get the exact same URL as above. When I paste that in a browser window I will not trigger the download, instead the above page will be loaded and I have to trigger the download manually in the same way as in the first place.
I've also tried using the network tab of the inspector to copy the request header of the request that is triggered when clicking the "SEARCH" button, but that URL is also the same as the one above.
Trying with C# I get the same result, the page itself is downloaded. My code looks as follows:
using (var client = new WebClient())
{
client.DownloadFile("http://insynsok.fi.se/SearchPage.aspx?reporttype=0&culture=sv-SE&fromdate=2016-05-30&tomdate=2016-06-03", "zipfile.zip");
}
My guess is that my code is correct, but how do I get the correct URL to be able to download the file directly?
ASP.net inserts a bunch of crap into the the page to make things like this particularly hard. (Validation tokens, form tokens, etc).
Your best bet is to use a python library called Mechanize, or if you want to stick to C# you can use Selenium or C# WebBrowser. This will fully automate visiting the page (you can render the C# WebBrowser invisible), then just click the button to trigger the download programatically.
On browsing local.com/test/document, I want to redirect to local.com/document/demo.pdf
It is getting redirected properly. i.e demo.pdf file is getting downloaded. But browser Url is not getting changed.
I am using following lines of code for redirection:
HttpContext.Current.Response.Redirect("local.com/document/demo.pdf", false);
context.ApplicationInstance.CompleteRequest();
Is this the correct behavior as we are redirecting to file? or is there a way where file can be downloaded and also browser url gets changed?
It is not possible using Response.Redirect(),because the it's a function and can do one task at a time.
You can use
<a href="http://xyz.pdf" **target="_self"**>Click Me</a>
Change the target _blank,it will redirect to a new tab.
You can also go with
<iframe src="xyz.pdf"></iframe>
As per an ASP.NET page, on click of a button the following actions should happen:
Download a file from server
Perform clean up actions like
Hide a Button
Set text to a label
Display the label
Disable a button
etc.
Now the download part happens through the following code:
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment; filename="+strFileName+".pdf");
Response.WriteFile(strBrowserPath);
And the remaining actions (Point 2) are done after.
Sadly, since Response.Clear()/Response.WriteFile() is used the remaining server side actions are not happening.
Any alternatives? Any fixes for this?
From the moment you use the post back action to send a file, at the same time you can not send and the page with the changes (disable button, change text, etc). There is no fixes for this because there is not a problem, is the way its works. You have one pipeline to send your response back. You ask for one http response and you get one http response, you can not have both update page and file.
Now, alternative. First of all is how you going to send the file. I suggest to use a handler with parameters like download.ashx?Dhwoload=filea.pdf and after the post back with the update on your page, you call the
window.location = "download.ashx?Dhwoload=filea.pdf";
javascript, with an alternative link as
if the file did not start to download automatically click here
and make your work.
relative:
What is the best way to download file from server
Error handling when downloading file from ASP.NET Web Handler (.ashx)
I'm working on an ASP.NET application. Currently when the user clicks "Save" on an item, we disable the Save button, change its text to "Saving..." and so on. This works fine, because the button causes a postback which results in a new page being loaded, on which the Save button is no longer disabled.
We now want to apply this to other actions: Publish (not much different from Save), Import (again, much like Save, but based on data the user has uploaded in a previous step), Export (downloads an XML file to the user) etc.
Export is causing me problems - the button stays disabled. I think this is because the server sends back an XML file rather than a new web page, and so the browser just displays the same page.
The server-side code is along the lines of
Response.Clear();
Response.BufferOutput = true;
Response.ContentType = "text/xml";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + whatever);
[push file contents into Response.OutputStream]
Response.End();
[No idea if this is good code or not - it's not mine - but it does the job :)]
Basically, I'd like to know either:
a way of making the server send a fresh page back in the response as well as the XML, thus re-enablnig the button in the same manner that the other pages do, or
a way of getting the browser/JS to re-enable the button once the file has been sent.
Looks like this should do it: essentially, set a cookie with the file response, and have the browser waiting for that cookie in order to unblock the page.
The problem probably is that you do not load a new page at all.
Since content disposittion is attachement, the browser will not reload the page but only save the return from the server to disk.
You need to reload the page some how but I have no really good idéa on how to do that after a file fetch.
There is a dirty hack. You can use setTimeOut method to enable and change back button caption/image.
So you can write a server side code similar to
btn.Attributes.Add("onclick", "this.disabled='true'; this.value='Processing...';_doPostback(this,null);setTimeout(function() { enable button logic....set the text /img of the button}, );");
The server side download attachment event will not be synch perfectly, but you can set timeout as 2-5 seconds depending on your server configuration.
Thank you!
I have a web page (asp.net) that compiles a package then redirects the user to the download file via javascript (window.location = ....). This is accompanied by a hard link on the page in case the redirect doesn't work - emulating the download process on many popular sites. When the IE information bar appears at the top due to restricted security settings, and a user clicks on it to download the file, it redirects the user to the page, not the download file, which refreshes the page and removes the hard link.
What is the information bar doing here? Shouldn't it send the user to the location of the redirect? Am I setting something wrong in the headers of the download response, or doing something else wrong to send the file in the first place?
C# Code:
m_context.Response.Buffer = false;
m_context.Response.ContentType = "application/zip";
m_context.Response.AddHeader("Content-Length", fs.Length.ToString());
m_context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}_{1}.zip", downloadPrefix, DateTime.Now.ToString("yyyy-MM-dd_HH-mm")));
//send the file
When a user agrees to download a file using the IE Information Bar, IE reloads the current page, not the page the file the user is trying to download. The difference is that, once the page is reloaded, IE will allow the download script to go through without prompting the user. I'm not sure what the thinking is on this from a design standpoint, but that's how it seems work.