Download file from protected website programmatically with Save As popup - c#

I use WebBrower of microsoft to log into a website. I have no problem with logging in. I don't have the downloadable file url because the file is generated automatically.
How should I download file with out Save As popup to show ?

Work with the DOM (document object model) of the website find your download link via traversing the DOM exposed by the browser control via it's property
Document
it is a tree of elements that your after-login page consists of. There should be some element that you should invoke click on it and then handle OnNavigating (https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.onnavigating(v=vs.110).aspx) event of the control to get the generated download URL.

Related

Get the raw binary data from an IE tab with a PDF open

I'm working on an Internet Explorer addon (IE11), using C#. I have by BHO & Button registered, and working. I'm trying to get the contents of the current tab when my button is pressed, WITHOUT issuing a second request for the resource.
With HTML, this is easy, I'm just using execScript to run some JavaScript which gets the page HTML.
Preferably, there would be a way to get the current pages content as a binary stream without issuing another request that works for HTML or PDF files (assuming the Adobe reader extension is installed), but I can't find any information about it.

Prevent WebBrowser from downloading PDF

I'm trying to download some PDF's using a WebBrowser. This URL contains an iframe with the src attribute set to this other URL, which in turn has a 302 redirect to this URL, which is a PDF file.
When I visit the first URL with a WebBrowser, it downloads and opens the file using Acrobat Reader. I would like to prevent this from happening because I'd like to automate the WebBrowser to visit many of these pages programmatically and don't want a bunch of Adobe Acrobat launching all over.
I have access to the DWebBrowserEvents2 and all of its events. One of the events getting fired was NavigateError, with the URL parameter pointing to the PDF file. I tried setting cancel to true, but the file was still being downloaded.
Is there any other way I can programmatically cancel the download?

Get the UNC of a file that the end user selects

I am trying to create a Sharepoint webpart for a user to browse to, and select, a file from a share on our file server. Then I need to create a link to this file to display in a list of links that will go onto our Sharepoint intranet page. I have created a custom web part using asp.net/c# in order to do this but I am stuck on how to get the UNC path to the document. From my understanding it wont work with an asp.net fileupload control or a html input element. What other options would there be? I really don't want the user to have to type in the whole path to the document. This needs to be a re-usable solution so that my users can create new lists of links to documents when they so desire. Thanks for any advice.
I don't think the full file path is supported or allowed via modern web browsers via the file upload control. What you would end up having to do is create something server-side that use a service account to access the file share, and then the client (web page) could call the server-side code as it traverses the file share until the user selects a file.
Example:
server: on load, here are the contents of "\server\home"
client: show the contents of subfolder "\server\home\pictures"
server: connects to "\server\home\pictures" and returns contents
client: selects "\server\home\pictures\foo.jpg"
Check out System.IO.Directory http://msdn.microsoft.com/en-us/library/system.io.directory(v=vs.100).aspx for ways to get listing of directory contents server-side (GetFiles, GetDirectories, GetFileSystemEntries, etc) and then you can return those results to the client.

Showing file on browser

I want have two links that to download and view the files
I am able to download the file directly. If user selects to view option I want to open that file in browser new tab.
How I will do it in asp.net mvc?
For download I have below action
public FileResult Document()
{
return File(filepath, contenttype,filename);
}
You can't control whether or not the file opens in a new tab or an existing tab from the server, this is controlled by setting the target attribute on the link definition.
You can control whether the browser downloads or attempts to display the file by setting the content type.

can .net WebBrowser control avoid popup SaveFileDialog instead of saving this file silently?

lately I got a problem on using .net WebBrowser control. when redirect to a file downloading, the WebBrowser control popup the SaveFileDialog, I don't know if there is a way avoid this to let me choose a filename and save it to some location.
Thanks for any helps.
You could handle the Navigating event, detect that it's a file download, make the request yourself with HttpWebRequest or WebClient, and cancel the navigating event within the handler.
You can install your own custom download manager by adding IServiceProvider to your WebBrowserSiteBase-derived class, which needs to be constructed in your webbrowser-derived class as the return value of the WebBrowser.CreateWebBrowserSiteBase virtual function.
In your download manager implementation you can write the file saving code. See https://code.msdn.microsoft.com/windowsdesktop/CSIEDownloadManager-8ab5d910 for an example to grab the download url. If the download url requires login, you need to grab session cookies. Check http://vbmhwb.sourceforge.net/ for an example.

Categories

Resources