从WebBrowser控件中打开文件?

时间:2022-11-11 23:07:46

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.

有谁知道是否可以通过WebBrowser组件中的链接在文件系统中打开文件?我正在编写一个小报告工具,在WebBrowser组件中我将HTML显示为摘要,其中包含指向更详细分析的链接,该分析在磁盘上保存为Excel文件。

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.

我希望用户能够在Web浏览器中单击该链接(当前只是标准的href标记,文件://path.xls作为目标)并获得打开文件的提示。如果我在IE中打开我的页面,这是有效的,但在WebBrowser控件(C#Windows Forms,.Net 2.0)中没有任何反应。

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?

我不知道我是否需要一些额外的权限/信任或某些 - 有没有人成功完成这个或任何人都可以建议如何调试这个?

2 个解决方案

#1


2  

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

我也测试了Ross的解决方案,它也适用于我。

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).

但这是另一种方法,而不是使用弹出一个要求您下载,打开或取消下载的对话框的内置功能,您可以在应用程序(而不是HTML页面)中使用您自己的C#代码来直接打开文件(或者做别的事情)。

As per Microsoft MSDN example:

根据Microsoft MSDN示例:

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" });
    }

}

#2


1  

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?

您是否指定了xls的完整路径?

#1


2  

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

我也测试了Ross的解决方案,它也适用于我。

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).

但这是另一种方法,而不是使用弹出一个要求您下载,打开或取消下载的对话框的内置功能,您可以在应用程序(而不是HTML页面)中使用您自己的C#代码来直接打开文件(或者做别的事情)。

As per Microsoft MSDN example:

根据Microsoft MSDN示例:

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" });
    }

}

#2


1  

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?

您是否指定了xls的完整路径?