Skip to main content Skip to footer

A SaveFileDialog for Silverlight 2

Before Microsoft release Silverlight2, we hope they could add a SaveFileDialog just like browser always shows. But

"We've been looking into this but after evaluating the security risks we are not planning on adding SaveFileDialog for Silverlight 2. We hope to enable a secure solution in a future release.

_Our recommendation is to create the file on the server and use the HyperlinkButton control to download it, or round trip the data from the client if necessary. This may not be 100% optimal but it does enable saving files to disk and uses the browser's established security model." --from Microsoft
_

So here is a alternative solution for SaveFileDialog. The solution is:

=client side==

1. dynamic create a form element;

2. encode byte array to Base64 string, and set it to one input field;

3. submit this form to server side handler(aspx or php);

=server side==

  1. get the Base64 string from Request, convert it to byte array;

5. rewrite byte array to Response;

6. append some additional info such as "Content-Type", "FileName";

=client side==

7. customer will get a save file dialog from the Browser;

How to use FpSaveFileDialog:

FpSaveFileDialog component has only two files:

>FpSaveFileDialog.cs --- source code of FpSaveFileDialog

>FpSaveFileDialogServerSideHandler.aspx --- server side handler. copy this file to your web site. and this handler can be written by any server side language(php and so on)  

Sample Codes to use FpSaveFileDialog:

  FpSaveFileDialog fsfg = new FpSaveFileDialog(new Uri("FpSaveFileDialogServerSideHandler.aspx", UriKind.Relative));  
  byte[] data = new byte[] {0x48, 0x65, 0x6C, 0x6C, 0x6F ,0x20 ,0x57, 0x6F ,0x72, 0x6C, 0x64 };  
  fsfg.Save(data, new FpSaveFileDialog.FpSaveFileDialogOption("test.txt"));

You will see:

All source codes are in attachment.

MESCIUS inc.

comments powered by Disqus