How to save a byte array to a file from silverlight
I have a SL 3 application connec开发者_如何学Goted to a WCF service. This service retrieves an array of bytes. I'd like to save that array as a pdf file using FileStream. The problem is that when the byte array is retrived, I get an exception when trying to show the SaveFileDialog because that action is initiated by the callback method and not from a user action, it seems. I'd like to know if there is any workaround for this. I already have the byte array, now I need to save it to a location specified by the user. No matter how... Any clue?
Thanks in advance.
Are you wiring to the method completed event of your async method call? See this
http://www.silverlightshow.net/items/Using-the-SaveFileDialog-in-Silverlight-3.aspx
Inside your call back method, you can implement the logic for writing to a file - first by opening the dialog, and then by getting the 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
}
精彩评论