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.
Related
I need to use C# Console Application.
I have a web Page URL
Inside the web page there is a button that downloads file by click
In View Source of the web page ,the button looks like this:
<a class="A" href="/myfiles/myfile.csv" target="_blank">הורד קובץ</a>
I should get the csv file in code by the URL.
How can I do it?
my question is similiar to this one , but not the same load a web page and click a button using c# I didnt find an answer yet.
Use WebClient class to download the HTML. Parse it manually to pick out the href. Use the WebClient class again to make a call to the href target. This should cause the file content to be downloaded as a response to your HTTP request.
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>
I am trying to download a file from a page that redirects itself using JS.
For example: https://wix.codeplex.com/downloads/get/119160
When that page is loaded in the browser the file download starts automatically using a JS function.
But when I try to download it using webrequest.create I only get the HTML of that page (119160).
Is there any clean way to download the file without inspecting the html/js manually?
Thanks,
Could use WebBrowser control (which can handle JS execution) then subscribe to the FileDownload event.
For a little earlier notice, you could subscribe to Navigating (which should get triggered once the JS kicks in).
I´m sending the value of a variable via POST to a PHP page in C#. I get the data stream from the server that has all the web page in HTML with the value of the POST. This information is stored in a string variable.
I would like to open a browser and show the web page (maybe using System.Diagnostics.Process.Start("URL")), without having to save it in a file, this is showing the page in the moment and, when the browser is closed, no file is stored in the server.
Any idea?
Drop a WebBrowser control into a new form webBrowser1 and set its DocumentTextProperty to your result html
webBrowser1.DocumentText = ("<html><body>hello world</body></html>");
source:
<html><body>hello world</body></html>
You aren't going to be able to do that in an agnostic way.
If you simply wanted to open the URL in a browser, then using the Process class would work.
Unfortunately, in your case, you already have the content from creating the POST to the server, and you really want to stream that response in your application to the browser.
It's possible among the some browsers, but it's not able to be done in an agnostic way (and it's complicated even when targeting a specific browser).
To complicate matters, you want the browser to believe that the stream you are sending it is really coming from the server, when in reality, it's not.
I believe that your best bet would be to save the response to the file system in a temp file. However, before you do, add the <base> tag to the file with the URL that the file came from. This way, relative URLs will resolve correctly when rendered in the browser.
Then, just open the temporary file in the browser using the Process class.
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.