Reputation: 9599
I have a Silverlight app where I want to do an export of some data. The file output format is most likely going to be PDF or Word. But let's assume I can generate the file contents appropriately. I want to be able to pop up a Save dialog for the user to save this data or open it directly in the program.
Now obviously I could just launch the user to a URL and do the export on the server, and change the MIME type of the response to be either Word or PDF. This would work just fine. However, the sticking point is that I already have the correct data on the client (including complex filters and the like) and recreating this data set on the server just to send it back to the client again seems silly if I can avoid it.
Is there any way to take an existing set of data in Silverlight and generate a Word or PDF file and get it onto the user's computer? I could also do it from JavaScript using browser interop from Silverlight. I don't want to use out-of-browser Silverlight.
Upvotes: 3
Views: 1257
Reputation: 112815
You need to use the SaveFileDialog
class. Note that due to Silverlight's security settings, the SaveFileDialog
needs to be opened as the result of a user event (e.g., a button click).
The dialog can be configured (if you want) using properties such as DefaultExt
or Filter
before you display it using the ShowDialog()
method.
The ShowDialog()
method will return true
if the user correctly specified a file and clicked OK. If this is the case, you can then call the SaveFileDialog.OpenFile()
method to access this file and write your data to it.
Example:
private void Button_Click(object sender, EventArgs e)
{
SaveFileDialog saveDialog = new SaveFileDialog();
if (saveDialog.ShowDialog())
{
System.IO.Stream fileStream = textDialog.OpenFile();
System.IO.StreamWriter sw = new System.IO.StreamWriter(fileStream);
sw.Write("TODO: Generate the data you want to put in your file");
sw.Flush();
sw.Close();
}
}
Upvotes: 2