Open pdf in web browser in C# console application - c#

Requirement:
I have a pdf file in my machine. Using console\windows application, i need to open that pdf file from browser itself. I am getting answers to open pdf file in browser using ASP.NET. But i need answer to open pdf in browser using console app.
I tried the below code:
string localURL = #"C:\MyLocation\apllication demo.pdf";
System.Windows.Controls.WebBrowser webbrowser = new System.Windows.Controls.WebBrowser();
webbrowser.Navigate(localURL);
But no use. It asusual opening in its default application.

This is simply because you are supplying a wrong path.
For browser, the path would look like this:
string localURL = "file:///C:/MyLocation/apllication%20demo.pdf"
Note that %20 is a space character,so perhaps use string.Replace(" ","%20") when building the Url.

Related

Interact with aspx page to download a file from C#

I'm attempting to automate a task I have of interacting with a website form to download a file.
In human terms, I select a label from a list of options, and then press a submit button. The website then automatically downloads a zip file to the downloads folder.
The actions described above have the net effect of running this code in a browser console:
$('#ListBox1').val($('#ListBox1 option:first')) && $('#Download_0').click()
My question is, how can I download this zip file by running the code from a C# program? Ideally, I would also be able to specify where the file downloads to, although I'm not sure if this is possible.
Please note, this question is not asking how to download a file using C# (see: How to download a file from a website in C# and .net - Downloading a file from a website using C#). No such file exists on the website for me to download. The resulting file is generated and downloaded automatically by the two actions above.
EDIT:
The following code does download a file, however, the file is 24kb (the zip file I'm expecting is around 8mb) and unopenable:
using (WebClient client = new WebClient())
{
client.DownloadFile("https://cdr.ffiec.gov/public/PWS/DownloadBulkData.aspx", AppDomain.CurrentDomain.BaseDirectory + "download.zip");
}
EDIT2:
What I'm attempting to do is download the most recent zip file available containing bulk call report data. The website selects the most recently available data automatically, but I have to manually select the type of file I want to download. As such, the web request won't be static and can't be hard-coded into the program, which is why I'm attempting to interact with the webpage.

Open a document(.doc,.pdf) using Google doc viewer - C#

I'm trying to open a document on server through google docs.
I can get the path and name of the file on the server.
And then on Button click event I wrote a method to open the file through google document reader.
string path = \\xxx-yyyyy-zzz\DocShare\sample1.doc //path of the file on server
Response.Redirect("http://docs.google.com/viewer?url=" + path);
When I run this code, I'm getting something like below
Can some one help me to find where did I go wrong? Is there any other method to open any document(ppt, doc, pdf etc) using google document reader.
TIA
What is \\xxx-yyyyy-zzz\DocShare\sample1.doc and where it is ?
A relative url dosen't specify a protocol & domain which makes the
browser to assume the document is referd from the same site/domain.
Please verify if you can access the document itself with the relative path.
http://docs.google.com/viewer?url=https://s3.amazonaws.com/scotchio/First-Node-App.pdf works for me as i can access the pdf. Try using the absolute url of the document
It doesn't work most likely because the file is not accessible from the Internet. The google doc previewer needs to be able to access the file in order to display it.

Downloading a file from url in c# windows forms

I have url like this
http://domainname.com/view/downloadfile?uname='ddd'&id=4
if we type the above url (not exactly the same ) in browser address bar it prompts open/save file dialog
my requirement is in button click without open/save dialog i need to download the file to my local disk location
Actually am working with webbrowser control .button is outside the webbrowser control
You could use the WebClient.DownloadFile method. Or if you don't want to save the file on the disk but manipulate it in memory you could use the WebClient.DownloadData method.

How can you right-click, save target as using WatiN?

I've seen various solutions to saving a file using WatiN.
My current issue is similar to the others described, I need to save a PDF file, but there's a registry key that tells IE to automatically open the PDF in a new window, rather than save it.
Ideally, I could just delete that registry key and move on. However, our security policy forbids me to do that (ugh.)
I'm looking for a way to find a link, right-click, and save-as using a built-in WatiN mechanism, rather than using mouse_event in user32.dll.
Is this possible?
Thanks for the help!
This is a very old question but just as a reference for other I'll post a solution to it.
I think that the approach that you are thinking of is incorrect. If you do have a link to a PDF you do have the address of the PDF (even if you have to go through other pages to form the final download URL, which you can emulate with Watin anyway) if you do have the address of the PDF file then you should just download it:
At this point you should get the link href string (that should contain the file you want to download)
using (WebClient client = new WebClient())
{
string DownloadFolder = #"C:\DownloadFolder";
if (Directory.Exists(DownloadFolder))
{
string downloadURL = "https://www.somesite.com/somepath/filename.pdf"; // this should come from the href on the a link.
client.DownloadFile(downloadURL, DownloadFolder + "\\somefilenam.pdf");
}
}

Displaying files in a directory

I am developing an ASP.NET 3.5 web application and I have a folder in the project which contains a list of documents which could be pdf or any of the MS office 2003 or 2007 supported file formats. I would like to display these files to my users as thumbnails (just like the way windows displays files). And when the user clicks on a file it has to prompt them to either save the file or open in the browser itself. How can I achieve this?
You can get the files like this (assuming /Documents)
string path = Server.MapPath(#"/Documents");
string[] files = System.IO.Directory.GetFiles(path);
And nou you only have to write some HTML generating code to display the files the way you want them displayed.
the answer at the following question seems to be RIGHT up your alley (ie the answer)
C# get thumbnail from file via windows api

Categories

Resources