/
SaveWindows.cs
60 lines (55 loc) · 2.25 KB
/
SaveWindows.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#region Copyright Syncfusion Inc. 2001-2016.
// Copyright Syncfusion Inc. 2001-2016. All rights reserved.
// Use of this code is subject to the terms of our license.
// A copy of the current license can be obtained at any time by e-mailing
// licensing@syncfusion.com. Any infringement will be prosecuted under
// applicable laws.
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using Windows.Storage;
using Windows.Storage.Pickers;
using Xamarin.Forms.Platform.UWP;
[assembly: Dependency(typeof(SaveWindows))]
class SaveWindows : ISave
{
public async Task SaveAndView(string filename, string contentType, MemoryStream stream)
{
stream.Position = 0;
//save the stream into the file.
if (Device.Idiom != TargetIdiom.Desktop)
{
StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
StorageFile outFile = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
using (Stream outStream = await outFile.OpenStreamForWriteAsync())
{
outStream.Write(stream.ToArray(), 0, (int)stream.Length);
}
if (contentType != "application/html")
await Windows.System.Launcher.LaunchFileAsync(outFile);
}
else
{
StorageFile storageFile = null;
FileSavePicker savePicker = new FileSavePicker();
savePicker.SuggestedStartLocation = PickerLocationId.Desktop;
savePicker.SuggestedFileName = filename;
if(contentType == "application/pdf")
{
savePicker.FileTypeChoices.Add("Adobe PDF Document", new List<string>() { ".pdf" });
}
storageFile = await savePicker.PickSaveFileAsync();
using (Stream outStream = await storageFile.OpenStreamForWriteAsync())
{
outStream.Write(stream.ToArray(), 0, (int)stream.Length);
}
//Invoke the saved file for Viewing.
await Windows.System.Launcher.LaunchFileAsync(storageFile);
}
}
}