forked from xamarin/Essentials
-
Notifications
You must be signed in to change notification settings - Fork 1
/
FilePickerViewModel.cs
205 lines (176 loc) · 6.27 KB
/
FilePickerViewModel.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Essentials;
using Xamarin.Forms;
namespace Samples.ViewModel
{
public class FilePickerViewModel : BaseViewModel
{
string text;
ImageSource image;
bool isImageVisible;
public FilePickerViewModel()
{
PickFileCommand = new Command(() => DoPickFile());
PickImageCommand = new Command(() => DoPickImage());
PickPdfCommand = new Command(() => DoPickPdf());
PickCustomTypeCommand = new Command(() => DoPickCustomType());
PickAndSendCommand = new Command(() => DoPickAndSend());
PickMultipleFilesCommand = new Command(() => DoPickMultipleFiles());
}
public ICommand PickFileCommand { get; }
public ICommand PickImageCommand { get; }
public ICommand PickPdfCommand { get; }
public ICommand PickCustomTypeCommand { get; }
public ICommand PickAndSendCommand { get; }
public ICommand PickMultipleFilesCommand { get; }
public string Text
{
get => text;
set => SetProperty(ref text, value);
}
public ImageSource Image
{
get => image;
set => SetProperty(ref image, value);
}
public bool IsImageVisible
{
get => isImageVisible;
set => SetProperty(ref isImageVisible, value);
}
async void DoPickFile()
{
await PickAndShow(PickOptions.Default);
}
async void DoPickImage()
{
var options = new PickOptions
{
PickerTitle = "Please select an image",
FileTypes = FilePickerFileType.Images,
};
await PickAndShow(options);
}
async void DoPickPdf()
{
var options = new PickOptions
{
PickerTitle = "Please select a pdf",
FileTypes = FilePickerFileType.Pdf,
};
await PickAndShow(options);
}
async void DoPickCustomType()
{
var customFileType =
new FilePickerFileType(new Dictionary<DevicePlatform, IEnumerable<string>>
{
{ DevicePlatform.iOS, new[] { "public.my.comic.extension" } }, // or general UTType values
{ DevicePlatform.Android, new[] { "application/comics" } },
{ DevicePlatform.UWP, new[] { ".cbr", ".cbz" } },
{ DevicePlatform.Tizen, new[] { "*/*" } },
{ DevicePlatform.macOS, new[] { "cbr", "cbz" } }, // or general UTType values
});
var options = new PickOptions
{
PickerTitle = "Please select a comic file",
FileTypes = customFileType,
};
await PickAndShow(options);
}
async void DoPickAndSend()
{
// pick a file
var result = await PickAndShow(PickOptions.Images);
if (result == null)
return;
// copy it locally
var copyPath = Path.Combine(FileSystem.CacheDirectory, result.FileName);
using (var destination = File.Create(copyPath))
using (var source = await result.OpenReadAsync())
await source.CopyToAsync(destination);
// send it via an email
await Email.ComposeAsync(new EmailMessage
{
Subject = "Test Subject",
Body = "This is the body. There should be an image attached.",
Attachments =
{
new EmailAttachment(copyPath)
}
});
}
async Task<FileResult> PickAndShow(PickOptions options)
{
try
{
var result = await FilePicker.PickAsync(options);
await Task.Delay(1000);
if (result != null)
{
Text = $"File Name: {result.FileName}";
if (result.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ||
result.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase))
{
var stream = await result.OpenReadAsync();
Image = ImageSource.FromStream(() => stream);
IsImageVisible = true;
}
else
{
IsImageVisible = false;
}
}
else
{
Text = $"Pick cancelled.";
}
return result;
}
catch (Exception ex)
{
Text = ex.ToString();
IsImageVisible = false;
return null;
}
}
async void DoPickMultipleFiles()
{
try
{
var resultList = await FilePicker.PickMultipleAsync();
if (resultList != null && resultList.Any())
{
Text = "File Names: " + string.Join(", ", resultList.Select(result => result.FileName));
// only showing the first file's content
var firstResult = resultList.First();
if (firstResult.FileName.EndsWith("jpg", StringComparison.OrdinalIgnoreCase) ||
firstResult.FileName.EndsWith("png", StringComparison.OrdinalIgnoreCase))
{
var stream = await firstResult.OpenReadAsync();
Image = ImageSource.FromStream(() => stream);
IsImageVisible = true;
}
else
{
IsImageVisible = false;
}
}
else
{
Text = $"Pick cancelled.";
}
}
catch (Exception ex)
{
Text = ex.ToString();
IsImageVisible = false;
}
}
}
}