michielpost.nl

Saving files in Silverlight 3

22-03-2009

How to use the Save File Dialog?
Silverlight 3 introduces the new SaveFileDialog, it let's you write files on a user's disk.

The basic code for opening a save file dialog is like this:

SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt =
".txt";
saveDialog.Filter =
"Text File|*.txt|All Files|*.*";

bool? open = saveDialog.ShowDialog();

if (open.HasValue && open.Value)
{
   
using (Stream fs = saveDialog.OpenFile())
   { 
      
byte[] fileByte = (new UTF8Encoding(true)).GetBytes("Your custom text input \r\n Next line");
      fs.Write(fileByte, 0, fileByte.Length);
      fs.Close();
   }
}

Download the sample project

The property DefaultExt lets you enter a default extension for the file that is being saved. If the user does not give an extension, for example only "filename" the file will be saved as "filename.txt" in this case, because the DefaultExt is .txt.

The Filter property works exactly the same as with the OpenFileDialog. It let's you choose between different file extensions. You need to give it a set of extensions seperated by the | character.

Always check the return value of the ShowDialog() method. It will give true when the user typed a file name and false when the dialog was closed or cancelled.


Saving a binary file, for example an Image which you downloaded
When the SaveFileDialog is closed, you don't have to save a file immediately. In this example, I first download an image and when that is finished I save the image to the file stream.

if (open.HasValue && open.Value)
{
   Uri uri = new Uri(http://silverlight.net/Themes/silverlight/images/logo.jpg);
   WebClient webClient = new WebClient();
   webClient.OpenReadAsync(uri);
   webClient.OpenReadCompleted +=
new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
}

//Download is complete, save the file
void
webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
   if (!e.Cancelled)
   {
      using (Stream fs = _asyncSaveDialog.OpenFile())
      {
         int length = Convert.ToInt32(e.Result.Length);
         byte[] byteResult = new byte[length];

         e.Result.Read(byteResult, 0, length);
         fs.Write(byteResult, 0, byteResult.Length);
         fs.Close();
      }
   }
}

Saving a file when running your Silverlight application out of the browser.

When I tried saving my file to the C:\ root, I got the following error message:

You don't have permission to save in this location.
Contact the administrator to obtain permission.

Would you like to save in the Documents folder instead?

However, it was possible to save to subfolders of the C drive and save to my D drive.
It's a little bit strange, because when running the application inside the browser I had no problems saving to the C:\ root.

Download the sample project

Comments


George 31-03-2009
Can't download a image from under site .It's work fine with http://silverlight.net/Themes/silverlight/images/logo.jpg, but when i change the address it give a exception. Please help me... George


Steve 15-05-2009
George The http://silverlight.net site has a http://silverlight.net/clientaccesspolicy.xml file. Any cross domain site you access needs this to allow you access. <?xml version="1.0" encoding="utf-8" ?> <access-policy> <cross-domain-access> <policy> <allow-from http-request-headers="Content-Type,SOAPAction"> <domain uri="*"/> </allow-from> <grant-to> <resource include-subpaths="true" path="/"/> </grant-to> </policy> </cross-domain-access> </access-policy>


New Comment

Name
E-mail (not published)
Comment
Enter the code shown: