I am trying to get the name of a file with an OpenFileDialog using OpenSilver.Controls.OpenFileDialog since the command I used before says it is obsolete and I replaced it with this new command.
But when I run this command, the project closes completely and I don’t get any error message.
I was able to successfully implement this functionality in a display using the code below:
#if HTML5_BROWSER
public async Task MouseLeftButtonDown1(object sender, System.Windows.Input.InputEventArgs e) {
try {
var fileDialog = new OpenSilver.Controls.OpenFileDialog();
bool? success = await fileDialog.ShowDialogAsync();
fileDialog.Filter = "Text documents (*.png)|*.jpg";
if (success == true) {
string filePath = fileDialog.File.FullName;
@Info.Trace("File full name: " + filePath);
}
}
catch (Exception ex) {
@Info.Trace("Error occurred: " + ex.Message);
}
}
// This callback is triggered only when running in the Web Client
public async Task BlazorControlLoaded(TBlazorControl control) {
// Add your code here
}
#endif
I’ve linked this task to a button, so the file dialog opens when it is clicked.
Everything works fine on my end when using the HTML5 client, as we’re leveraging the OpenSilver library.
Did this issue occur for you when using the WPF client? If not, and you’re seeing it in the Web Client, it’s possible you’re running an older version. If that’s the case, you can download our latest release here.
That being the case, I recommend using the TOpenFileDialog() class.
It’s a new class created for FrameworX, and it has a few quirks—such as the ability to run asynchronously
Below is a small code snippet showing how I’ve implemented this solution. I hope it helps, and feel free to test it out:
public async Task MouseLeftButtonDown1(object sender, System.Windows.Input.InputEventArgs e) {
try {
var dialog = new TOpenFileDialog();
dialog.Filter = "Image files (*.jpg;*.png)|*.jpg;*.png";
bool? success = await dialog.ShowDialogAsync();
if (success == true) {
string filename = dialog.File.Name;
@Info.Trace("Here's the file name: " + filename);
}
}
catch (Exception ex) {
@Info.Trace("There was an error: " + ex.Message);
}
}