2008-11-28 96 views
3

有谁知道,如果有可能通过在web浏览器组件的链接来打开文件系统中的文件?我正在编写一个小报告工具,在该工具中,我在WebBrowser组件中以HTML格式显示摘要,并提供指向更详细分析的链接,该分析作为Excel文件保存在磁盘上。从WebBrowser控件内打开文件?

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

我不知道如果我需要一些额外的权限/信托或诸如此类 - 有没有人成功地做到了这一点还是会有人提出如何调试呢?

回答

1

我只是看起来像 < A HREF =链接尝试此 “文件:/// C:\ TEMP \ browsertest \ BIN \调试\ testing.xls” >测试</A >

并按预期工作。

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

2

我还测试了罗斯的解决方案,它也为我工作。

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

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

}