views:

3509

answers:

2

Does anyone know if it's possible to open a file in the file system via a link in a WebBrowser component? I'm writing a little reporting tool in which I display a summary as HTML in a WebBrowser component with a link to a more detailed analysis which is saved as an Excel file on disk.

I want the user to be able to click that link within the web-browser (currently just a standard href tag with file://path.xls as the target) and get a prompt to open the file. If I open my page in IE, this works, but in the WebBrowser control (C# Windows Forms, .Net 2.0) nothing happens.

I don't know if I need some additional permissions/trust or somesuch - has anyone done this successfully or could anyone suggest how to debug this?

A: 

I just tried this with a link that looks like <a href="file:///C:\temp\browsertest\bin\Debug\testing.xls">Test</a>

and it worked as expected.

Are you specifying the full path to the xls?

A: 

I also tested Ross's solution and it worked for me too.

But here's another approach, instead of using the built-in functionality that popups a dialog box asking you to download, open or cancel the download, you can use your own C# code in your application (not the HTML page) to directly open the file (or maybe do something else).

As per Microsoft MSDN example:

using System;
using System.Windows.Forms;
using System.Security.Permissions;

[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class Form1 : Form
{
    private WebBrowser webBrowser1 = new WebBrowser();
    private Button button1 = new Button();

    [STAThread]
    public static void Main()
    {
        Application.EnableVisualStyles();
        Application.Run(new Form1());
    }

    public Form1()
    {
        button1.Text = "call script code from client code";
        button1.Dock = DockStyle.Top;
        button1.Click += new EventHandler(button1_Click);
        webBrowser1.Dock = DockStyle.Fill;
        Controls.Add(webBrowser1);
        Controls.Add(button1);
        Load += new EventHandler(Form1_Load);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        webBrowser1.AllowWebBrowserDrop = false;
        webBrowser1.IsWebBrowserContextMenuEnabled = false;
        webBrowser1.WebBrowserShortcutsEnabled = false;
        webBrowser1.ObjectForScripting = this;
        // Uncomment the following line when you are finished debugging.
        //webBrowser1.ScriptErrorsSuppressed = true;

        webBrowser1.DocumentText =
            "<html><head><script>" +
            "function test(message) { alert(message); }" +
            "</script></head><body><button " +
            "onclick=\"window.external.Test('called from script code')\">" +
            "call client code from script code</button>" +
            "</body></html>";
    }

    public void Test(String message)
    {
        MessageBox.Show(message, "client code");
    }

    private void button1_Click(object sender, EventArgs e)
    {
        webBrowser1.Document.InvokeScript("test",
            new String[] { "called from client code" });
    }

}
Nazgulled