2011-03-03 50 views
1

我想使用openfiledialog来上传文件,但是当我编写下面的代码时会触发“安全例外”,即“对话框必须是用户启动的。”无法打开Silverlight中的openfileDialog

btn_click() 
{ 
    OpenFileDialog fileDialog=new OpenFileDialog();    
    fileDialog.Multiselect = false; 
    fileDialog.Filter = "All Files|*.*"; 
    bool? retval = fileDialog.ShowDialog(); 
    if (fileDialog.ShowDialog()==false){ 
    Stream strm = fileDialog.File.OpenRead(); 
    byte[] Buffer = new byte[strm.Length]; 
    strm.Read(Buffer, 0, (int)strm.Length); 
    strm.Dispose(); 
    strm.Close(); 
    Uploadfile file=new Uploadfile(); 
    file.FileName = fileDialog.File.Name; 
    file.File = Buffer; 
    po.fileUploadAsync(file); 
    } 
+1

Anil - 请将您的编辑回滚到第二个版本,然后**将代码添加到问题中,而不是替换原始文本。 – ChrisF 2011-03-03 11:34:37

+0

我已经为你做了。 – badp 2011-03-03 12:07:54

回答

1

当你得到美国的打开文件对话框的异常可以当应用程序在浏览器中,并与限信任运行只能从用户启动的操作激活。

你想达到什么目的?

最简单的解决方案是向您的用户界面添加一个按钮,允许用户控制何时发生此过程。

+0

感谢您的回复。我在Silverlight中创建了文件上传用户控件,并在浏览按钮上单击了我编写的上面的代码。但无法打开对话框。 – Anil 2011-03-03 10:31:53

+0

@Anil - 你可以发布一些更多的代码。 – ChrisF 2011-03-03 10:34:07

+0

private void Browse_Click(object sender,RoutedEventArgs e){OpenFileDialog fileDialog = new OpenFileDialog(); fileDialog.Multiselect = false; fileDialog.Filter =“所有文件| *。*”; bool? dialogresult = fileDialog.ShowDialog(); (fileDialog.ShowDialog()== false){0} {0} {0}流strm = fileDialog.File.OpenRead(); byte [] Buffer = new byte [strm.Length]; strm.Read(Buffer,0,(int)strm.Length); (); strm.Close(); Uploadfile file = new Uploadfile(); file.FileName = fileDialog.File.Name; file.File = Buffer; po.fileUploadAsync(file); } – Anil 2011-03-03 10:39:46

0
OpenFileDialog dlg = new OpenFileDialog(); 
dlg.Filter = "Text Files (*.txt)|*.txt"; 

if (dlg.ShowDialog() == DialogResult.OK){ 

    using (StreamReader reader = dlg.SelectedFile.OpenText()) 
    // Store file content in 'text' variable  
    string text = reader.ReadToEnd(); 
} 

}