|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Text; |
| 4 | +using Windows.Storage; |
| 5 | +using Windows.Storage.Pickers; |
| 6 | +using Windows.Storage.Streams; |
| 7 | +using Windows.UI.Popups; |
| 8 | + |
| 9 | +namespace SfDataGridSample; |
| 10 | + |
| 11 | +public partial class SaveService |
| 12 | +{ |
| 13 | + public async partial void SaveAndView(string filename, string contentType, MemoryStream stream) |
| 14 | + { |
| 15 | + StorageFile stFile; |
| 16 | + string extension = Path.GetExtension(filename); |
| 17 | + //Gets process windows handle to open the dialog in application process. |
| 18 | + IntPtr windowHandle = System.Diagnostics.Process.GetCurrentProcess().MainWindowHandle; |
| 19 | + if (!Windows.Foundation.Metadata.ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons")) |
| 20 | + { |
| 21 | + //Creates file save picker to save a file. |
| 22 | + FileSavePicker savePicker = new(); |
| 23 | + if (extension == ".xlsx") |
| 24 | + { |
| 25 | + savePicker.DefaultFileExtension = ".xlsx"; |
| 26 | + savePicker.SuggestedFileName = filename; |
| 27 | + //Saves the file as xlsx file. |
| 28 | + savePicker.FileTypeChoices.Add("XLSX", new List<string>() { ".xlsx" }); |
| 29 | + } |
| 30 | + else if(extension == ".pdf") |
| 31 | + { |
| 32 | + savePicker.DefaultFileExtension = ".pdf"; |
| 33 | + savePicker.SuggestedFileName = filename; |
| 34 | + |
| 35 | + //Saves the file as Pdf file. |
| 36 | + savePicker.FileTypeChoices.Add("PDF", new List<string>() { ".pdf" }); |
| 37 | + } |
| 38 | + |
| 39 | + WinRT.Interop.InitializeWithWindow.Initialize(savePicker, windowHandle); |
| 40 | + stFile = await savePicker.PickSaveFileAsync(); |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + StorageFolder local = ApplicationData.Current.LocalFolder; |
| 45 | + stFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting); |
| 46 | + } |
| 47 | + if (stFile != null) |
| 48 | + { |
| 49 | + using (IRandomAccessStream zipStream = await stFile.OpenAsync(FileAccessMode.ReadWrite)) |
| 50 | + { |
| 51 | + //Writes compressed data from memory to file. |
| 52 | + using Stream outstream = zipStream.AsStreamForWrite(); |
| 53 | + outstream.SetLength(0); |
| 54 | + //Saves the stream as file. |
| 55 | + byte[] buffer = stream.ToArray(); |
| 56 | + outstream.Write(buffer, 0, buffer.Length); |
| 57 | + outstream.Flush(); |
| 58 | + } |
| 59 | + //Create message dialog box. |
| 60 | + MessageDialog msgDialog = new("Do you want to view the document?", "File has been created successfully"); |
| 61 | + UICommand yesCmd = new("Yes"); |
| 62 | + msgDialog.Commands.Add(yesCmd); |
| 63 | + UICommand noCmd = new("No"); |
| 64 | + msgDialog.Commands.Add(noCmd); |
| 65 | + |
| 66 | + WinRT.Interop.InitializeWithWindow.Initialize(msgDialog, windowHandle); |
| 67 | + |
| 68 | + //Showing a dialog box. |
| 69 | + IUICommand cmd = await msgDialog.ShowAsync(); |
| 70 | + if (cmd.Label == yesCmd.Label) |
| 71 | + { |
| 72 | + //Launch the saved file. |
| 73 | + await Windows.System.Launcher.LaunchFileAsync(stFile); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | +} |
0 commit comments