obtain downloaded file path from WebBrowser component - c#

Using the WebBrowser component in C#, is it possible to obtain the file path of a user downloaded file?
For example, a user is browsing an arbitrary website using the WebBrowser then clicks a link to download, say, a PDF. The default download manager pops-up and prompts the user to save the file and the user can download the file to a location of their choice; but I do not know where they have saved that file.
Is this actually possible using the WebBrowser control?

You can try this:
private void webBrowser1_Navigating(object sender,
WebBrowserNavigatingEventArgs e)
{
e.Cancel = true;
WebClient client = new WebClient();
client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);
client.DownloadDataAsync(e.Url);
}
void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
string filepath = textBox1.Text;
File.WriteAllBytes(filepath, e.Result);
MessageBox.Show("File downloaded");
}

When you host the web browser component, you can handle all the events. So for example, you can handle the right click, override the menu and show your custom menu.
So When the user clicks on a link or right clicks, you can show your own dialog instead of the system dialog and then you can have the path of the downloaded file from your own dialog.

Related

Open Access file with button click

Is it possible to open my Microsoft Access file with just a button click event?
The tutorials I read online has to go through OpenFileDialog but I do not want that. I want to open the file directly with a click of a button.
File Location: C:\Users\User\Documents\MedicalRecord.accdb
You can use Process.Start(), it's the same thing as double-clicking your file directly.
private void button1_Click(object sender, EventArgs e)
{
Process.Start(#"C:\Users\User\Documents\MedicalRecord.accdb");
}
Yes, it is possible, instead of using open file dialog's file name, use the file location you want

How to automatically accept the "Open/Save Dialog" in a WebBrowser Control?

I am working on a c# Windows utility, which is opening a file using webbrowser control.
webBrowser1.Navigate("URL");
URL is actually an attachment, which is showing as "Open" "Save" dialog
private void WebBrowser1_FileDownload(Object sender, EventArgs e)
{
textbox1.text ="filedownload event fired!!"
}
can I do somthing so that i cancel the file open/save dialog in the background.
As I wanted to automate the process and don't want user to get popped up message.

Open YouTube Embedded Player link in default browser

I'm hosting YouTube Embedded Player in C# WebBrowser control. When I click button "Watch on YouTube" IE opens. Is there any way to open the link in default web browser, for instance, Chrome?
Like this for example:
private void browser_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
System.Diagnostics.Process.Start(e.Url.ToString());
e.Cancel = true;
}
Explanation: change the navigating handler to process the URL and fire the link in the diagnostics (which will open the URL in the user’s default browser).

How to hide loading animation when an open/save dialog is showm to the user?

I have an export button in my application.
When I click the button, it posts back to the server and generates an Excel file.
I want to show a loading gif during the process, but when the open/save dialog shows, I want to hide the gif.
The problem is that I use Response.End to display the file open/save dialog.
Does anyone knows how can I call a client side function after Response.End, or anyone has another solution how can I hide the gif and let the user work?
here is a piece of my code:
protected void Page_Load(object sender, EventArgs e)
{
//starts the loading gif animation.
btnExport.Attributes.Add("onclick", "setTimeout(\"UpdateImg();\",300);");
}
protected void btnExport_Click(object sender, EventArgs e)
{
//generates the file
Response.ContentType = "application/vnd.openxmlformats- officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=ExcelDemo.xlsx");
Response.BinaryWrite(pck.GetAsByteArray());
Response.Flush();
//Response.End();
HttpContext.Current.ApplicationInstance.CompleteRequest();
//HERE I WANT TO STOP THE ANIMATION AND GO BACK TO THE PAGE!
}
Thank you,
Inbal.
This way you can not do that.
When you start send your data to the file and open a save dialog, your web page have lost the control and you can not do anything any more, the data are all go to the file save.
What other and in my opinion better, options you have.
You can use a handler .ashx to create and send your file, and you give the link to your page with the parameters you need to make the file.

click on a link event webbrowser c#?

I want to open a link in IE browser from the web browser control in my winform ... the webbrowser control navigate to a page when the user click on a link it opens in the internet explorer browser.
Basically handle the WebBrowser.Navigating Event, cancel the navigation and open up the url in explorer like this:
private void WebBrowserNavigating(object sender, WebBrowserNavigatingEventArgs e)
{
e.Cancel = true;
Process.Start("iexplorer.exe", e.Url);
}

Categories