I've been messing around with the Open File Dialog in Silverlight 2.0 Beta 2.
The open file dialog (OFD) is the only method that a user can access the local file system in a Silverlight Application (outside of Isolated Storage).
The cool thing about it is is that you can work with not just only text files but other types of files too.
Single or Multiple Files
The OFD supports both a single file select mode or multi file select via the (Multiselect) property.
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = false;
Filtering to MP3 or WMA files
// Initialise
OpenFileDialog ofd = new OpenFileDialog();
// Filter to wma and mp3
ofd.Filter = "MP3 Files (*.mp3)|*.mp3|WMA Files (*.wma)|*.wma";
ofd.Multiselect = false;
// Show the open file dialog
ofd.ShowDialog();
Playing Local Media Files
The following bit of code will set the source of a medialement to the stream of the selected file. In this case it allow you to play a local video or audio file.
// Initialise
OpenFileDialog ofd = new OpenFileDialog();
// Filter to wma and mp3
ofd.Filter = "MP3 Files (*.mp3)|*.mp3|WMA Files (*.wma)|*.wma";
ofd.Multiselect = false;
// Show the open file dialog
ofd.ShowDialog();
// Do we have a selected file
if (ofd.SelectedFile != null)
{
// Set the source as the local file
mediaPlayer.AutoPlay = true;
mediaPlayer.SetSource(ofd.SelectedFile.OpenRead());
}
Text Files
If you wanna be a little more traditional in your use of the open file dialog, you can use OpenText() rather than OpenRead() to allow you to interact with the text file. An advantage of OpenText is that it gives you a StreamReader so you have access to ReadToEnd() etc.
private void btnOpen_Click(object sender, RoutedEventArgs e)
{
// Initialise
OpenFileDialog ofd = new OpenFileDialog();
// Filter to wma and mp3
ofd.Filter = "MP3 Files (*.mp3)|*.mp3|WMA Files (*.wma)|*.wma";
ofd.Multiselect = false;
// Show the open file dialog
ofd.ShowDialog();
// Do we have a selected file
if (ofd.SelectedFile != null)
{
string myText = ofd.SelectedFile.OpenText().ReadToEnd();
}
}
If you want to select files then you should use the SelectedFiles property rather than SelectedFile and you will be able to enumerate through all the selected files.
Uses of the Open File Dialog
In a normal web scenario using a Silverlight Control to perform uploading of files is a much richer experience than using the equivalent Html Control. You have the ability to select, process and upload multiple files at once.
The ability to perform client side validation of a file (CSV validation, XML validation) is a great feature and should encourage great take up in web scenarios.
The only thing I really want now is a Save File Dialog.
5 comments:
could it get the parent object of selected file? if so, it can control the whole file system?
Thanks Chris.
I plan to try your blog title out, once I get a clean install of SL2 B2, using F# .
Hi Chris --
Great post and cool samples. Is it okay with you if I post some of these (like your Open File Dialog, and Wee Mee, and others) to http://silverlightnuggets.com ?
Thanks,
Rajeev
rgoel@silverlightnuggets.com
rajeev,
Go for it. Anything I post up, I'm happy for others to use.
My app is going to open one xml file at a time, apply a transform, and then show the result in a text box on the page.
You've shown how to locate the file and apply a filter. Phew, that makes things a lot easier. Now I want to learn how to pass the file path to an XML reader, call an XSL, and render the result in a text box.
I'll see if I can find out myself. Meantime, thanks for getting me started!
Post a Comment