2008-11-28 132 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" }); 
    } 

}