How to save an array of bytes to a file from silverlight

I have an SL 3 application connected to a WCF service. This service retrieves an array of bytes. I would like to save this array in pdf format using FileStream. The problem is that when the byte array is retrieved, I get an exception when trying to show SaveFileDialog, because this action is triggered by the callback method, not the user action. I would like to know if there is any workaround for this. I already have an array of bytes, now I need to save it in a user-specified location. No matter how ... Any clue?

Thanks in advance.

+4
source share
1 answer

Are you connecting to the completed event method of your asynchronous method call? See This

http://www.silverlightshow.net/items/Using-the-SaveFileDialog-in-Silverlight-3.aspx

Inside the callback method, you can implement the logic for writing to a file - first by opening a dialog, and then receiving a pointer to the file stream, as shown below.

try { byte[] fileBytes = //your bytes here SaveFileDialog dialog=new SaveFileDialog(); //Show the dialog bool? dialogResult = this.dialog.ShowDialog(); if (dialogResult!=true) return; //Get the file stream using ( Stream fs = ( Stream )this.dialog.OpenFile() ) { fs.Write( fileBytes, 0, fileBytes.Length ); fs.Close(); //File successfully saved } } catch ( Exception ex ) { //inspect ex.Message } 
+6
source

Source: https://habr.com/ru/post/1299961/


All Articles