11128

Saving Files in silverlight and asynchronous call backs

In Silverlight you have to save a file using the save file dialog.

You can only open this dialog from a user event aka a button click

I’m returning the data of a file from a web service call asynchronously

How do i save this to file?

If i ask them before the service call i can’t use the stream after the data comes back.

If i ask them after i can’t open the save file dialogue.

It’s a bit of chicken and the egg situation.

Thanks.

<strong>update</strong>

i want to be able to save the users computer where they spectifiy not the silverlight isolated storage.

Answer1:

Open a SaveFileDialog from a user event, then keep a reference to this dialog around. Make your web service call, then in the handler for this call, call the OpenFile() method on the SaveFileDialog. Use the stream returned by this method to write to your file.

private SaveFileDialog _mySaveDialog; private void Button_Click(object sender, EventArgs e) { _mySaveDialog = new SaveFileDialog(); // Configure the dialog and show it here... } // call this method from the handler for your web service call private void Save(string toSave) { Stream fileStream = _mySaveDialog.OpenFile(); // Write to the file here... }

If you'd like an even more detailed example that uses this same technique, see here.

Answer2:

if i get your question correct, here is proposed solution.

off course there would be a button which will open the save button. but that button would be enabled when the service call completes. you can save that data temporary into isolated storage. Enable the button now. on clicking that open the save diag box,

here is the code to save data into isolated storage temporary.

public static void SaveLog(string data) { using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName, FileMode.Append, FileAccess.Write, isf)) { using (StreamWriter sw = new StreamWriter(isfs)) { try { sw.Write(data); } finally { sw.Close(); } } } } }

Recommend

  • How to override download process in Chrome Extension
  • C# SaveFileDialog
  • File not downloaded completely
  • Download file from web and then save with a save file dialog box?
  • Visual Studio Attach to Process - change default automatic code type (Silverlight instead of Script)
  • int to System.Windows.GridLength - VB.NET
  • Silverlight and binding to anonymous types
  • Reading adjacency lists with isolated nodes using igraph
  • HTML5 embed tag - event attributes not firing (onended, onpause etc)
  • Cordova 3.6: how can I extract GPS Exif data from photo library in Android?
  • JQuery: select paragraphs that only contain “ ” [duplicate]
  • How can I migrate my WP8 application to universal when it uses a local linq to sql db?
  • fabricator with hstore attribute
  • Rapidly iterating Silverlight development
  • Angular DatePicker - Multiple directives > [datepicker, datepicker]
  • Data Type of Columns in a List - R
  • How to get or calculate size of Azure File/Share or Service
  • Whats the best way of persisting data to Isolated Storage on Windows Phone 7?
  • Where to save the local DB created for iphone app?
  • get passwords from chrome
  • iOS - Changing frame of a subview of a subclassed UIView (using storyboard with auto layout enabled)
  • Does the Azure table storage API cache results?
  • Unexpected token ILLEGAL while running node.js mocha test
  • cygwin cannot exec 'git-add--interactive' permission denied
  • MS Access - How to change the linked table path by amend the table
  • Ajax calls do not work in IE unless you fiddle with security settings
  • Alternative to overridePendingTransition() - Android
  • Rails Find when some params will be blank
  • MongoError: Incorrect arguments
  • C++ Partial template specialization - design simplification
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • JavaScriptCore crash on iOS9
  • Cassandra Data Model
  • Can I make an Android app that runs a web view in Chrome 39?
  • SVN: Merging two branches together
  • Transpose CSV data with awk (pivot transformation)
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • LevelDB C iterator
  • Linking SubReports Without LinkChild/LinkMaster
  • How to get NHibernate ISession to cache entity not retrieved by primary key