0

In my internet explorer add-on I want to detect when user begin to download a file, I with FileDownload event but it doesn;t fire when user downloads file but when navigating.

For illostating the secenario I created a small console app (If you want to test it on your computer):

  1. Add reference to SHDocVw dll (location at C:\Windows\System32)
  2. Create console app with code below
  3. Enable yor app to interact with Internet explorer as I wrote bellow.

The code:

    static void Main(string[] args)
    {
        SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
        object Empty = 0;
        object URL = "http://www.orimi.com/pdf-test.pdf";
        IE.BeforeNavigate2 += WebBrowser_BeforeNavigate21;
        IE.FileDownload += IE_FileDownload;
        IE.Visible = true;
        Thread.Sleep(60000);
        IE.Quit();
    }

    public void OnBeforeNavigate2(object sender, ref object URL,
                                      ref object Flags, ref object Target,
                                      ref object PostData, ref object Headers,
                                      ref bool Cancel)
    {
        Console.WriteLine("BeforeNavigate2 fired!");
    }

    private static void IE_FileDownload(bool ActiveDocument, ref bool Cancel)
    {
        Console.WriteLine($"{ActiveDocument} {DateTime.Now}");
        Cancel = true;

    }

Enable IE to interact with app:

1.1 In inernet explorer open "Internet Options" 1.2 UnCheck the marked checkboxes under "Advance" tab

enter image description here

spez
  • 409
  • 8
  • 21
  • Is there any error when you debug the project? I found that you have posted a [question](https://stackoverflow.com/questions/60248843/hello-world-internet-explorer-extension-add-on) before. Is that the same project? I also find [another solution](https://stackoverflow.com/questions/1106377/detect-when-browser-receives-file-download) using javascript to detect the file download. If possible you could also give it a try. – Yu Zhou Feb 21 '20 at 09:51
  • @YuZhou No errors, if so I would say. The linked solusion assuming I have access to server side code, that's not the same case with extensions. – spez Feb 25 '20 at 13:31
  • I saw you asked several similar questions and I answered the other one also. You could check my answer of [this thread](https://stackoverflow.com/questions/60372634/detect-file-download-with-internet-explorer) and take it into consideration. I also suggest that you can focus on one thread as the similar threads will confuse us. – Yu Zhou Feb 26 '20 at 08:46

1 Answers1

1

It can be done with BeforeNavigate2 event like:

SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer();
IE.BeforeNavigate2 += IE_BeforeNavigate2;

when user download file the following method invoked:

static void IE_BeforeNavigate2(object pDisp, ref object URL, 
ref object Flags, ref object TargetFrameName, ref object PostData, 
ref object Headers, ref bool Cancel)
{
    Console.WriteLine("Event: IE_BeforeNavigate2");
}
Nehorai Elbaz
  • 2,412
  • 8
  • 17