How to save a byte array to a file from silverlight

david-shm
david-shm used Ask the Experts™
on
Hello Everybody,

I have a SL 3 application connected 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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
>>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?


Don't think you can bypass this because is security issue.
Can you bring up SaveFileDialog when user click the button?
Another way to save to Isolated Storage and i don't think this is the way you want


Author

Commented:
Hello,

No. I tryed to show a SaveFileDialog when the user clicks the button but the exception still happens.
I thought that was possible...

Commented:
don't take a break point on the line DialogSave.ShowDialog() or before, break point after the line
DialogSave.ShowDialog()

SaveFileDialog DialogSave = new SaveFileDialog();

            if (DialogSave.ShowDialog() == true)
            {
               ...
            }
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

Author

Commented:
Hello EDDYKT,

The excution thread doesn't reach any point beyond the if, because DialogSave.ShowDialog() throws an exception.
Kind regards,

David
Thing is calling SaveFileDialog should be only user initiated, so basicaly just store byte array in your app when it arived and dysplay dialog on bytes recived callback. With button that calls SaveFiledialog.

This article displays another way of doing your task: http://www.silverlightshow.net/items/Using-the-SaveFileDialog-in-Silverlight-3.aspx
Commented:
>>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?


When the user click the button you need to call

private Stream fs = null; // declare this on the top of your class

SaveFileDialog DialogSave = new SaveFileDialog();

            if (DialogSave.ShowDialog() == true)
            {
               fs = (Stream)DialogSave.OpenFile();
            }

When call back comes
fs.Write(e.Data, 0, (int)e.Length);
fs.Flush();
fs.Close();
fs = null;

Author

Commented:
Hi,

I could resolve it based on this: http://stackoverflow.com/questions/2177968/how-to-save-a-byte-array-to-a-file-from-silverlight
But your suggestion for the callback is a very good idea. Let me try it and I'll let you know ASAP.

Commented:
>>I could resolve it based on this: http://stackoverflow.com/questions/2177968/how-to-save-a-byte-array-to-a-file-from-silverlight

don't think you can call DialogSave.ShowDialog()  on your callback
Util it's initiated by user button click and no iterfaces used to get to SaveFileDialog

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial