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>
Related
From what I can tell, the site requires a button click, which runs some Javascript and then sends a POST request. The POST request returns a pdf. All the solutions I've found for downloading a file either use WebClient (but I don't have a URL for the pdf) or HTTPRequest (which can't invoke a click).
I can get to the point of invoking the click with WebBrowser, and I can see using Fiddler that the pdf is getting returned in the site's response, but I have no idea how to get it onto my machine.
I was able to solve this using Selenium, with the following config settings passed into the Firefox driver:
profile = Firefox.FirefoxProfile()
profile.SetPreference("browser.download.dir", saveDir)
profile.SetPreference("browser.download.folderList", 2)
profile.SetPreference("browser.helperApps.neverAsk.saveToDisk","application/pdf")
profile.SetPreference("pdfjs.disabled",True)
profile.SetPreference("browser.tabs.remote.autostart", False)
profile.SetPreference("browser.tabs.remote.autostart.1", False)
profile.SetPreference("browser.tabs.remote.autostart.2", False)
profile.SetPreference("browser.tabs.remote.force-enable", "false")
driver = Firefox.FirefoxDriver(profile)
Where saveDir is the target download directory. The first half of these config settings are to make Firefox download without a prompt, and the value for browser.helperApps.neverAsk.saveToDisk is a MIME type. The second half of the configs prevent Firefox from crashing when driver.Quit() is called.
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.
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.
I am using Response.Redirect in ASP .NET C# application, to redirect to a different web page based on success or failure.
But Response.Redirect is not working. Instead or redirecting to the new link, the body or the contents of the target web page is appended to the existing URL. I tried it for a simple HelloWorld page and still its not working. For ex:
If I am in home page: http://www.example.com/test/default.aspx
and if I want to redirect to HelloWord html page, then the final URL would be
Response.Redirect("~/../hello.html"); but I get http://www.example.com/<p>Hello%20World!!</p>
Due to the improper URL, I am getting "Access Denied Error".
Thanks for the help in advance.
You should use:
Response.Redirect("~/hello.html");
if your hello.html is in the application root directory.
Or if you want a relative parent directory to the current page:
Response.Redirect("../hello.html");
~ references the application root directory, so with "~/.." you are trying to access a parent of the root directory, that is not allowed.
I think you should use Server.Transfer() instead. It will redirect you to a new page. For example:
Server.Transfer("Home.html")
if the page is in the same directory, otherwise just use relative link
You cannot redirect to a file that is outside of an IIS site, as "one level up from the app-root" probably is.
Add the following code to the Page_Load event:
Response.Redirect("http://www.microsoft.com");
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.