Reputation: 37
I want to use openfiledialog to upload file,but when I write following code Security exception is fired that is "Dialogs must be user-initiated."
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);
}
Upvotes: 1
Views: 989
Reputation: 5561
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();
}
}
Upvotes: 0
Reputation: 137148
As the exception you're getting states the open file dialog can only be activated from a user initiated action when the application is run in the browser and with restricted trust.
What are you trying to achieve?
The simplest solution is to add a button to your UI that allows the user to control when this process happens.
Upvotes: 1