Exemplo n.º 1
0
        private async Task DownloadFile()
        {
            if (fileSelected == null)
            {
                await MyLogger.Alert("Please select a cloud folder first");
            }
            else
            {
                switch (Device.RuntimePlatform)
                {
                case Device.Windows:
                    if (fileSelected.IsFolder)
                    {
                        var picker = new Windows.Storage.Pickers.FolderPicker()
                        {
                            ViewMode = Windows.Storage.Pickers.PickerViewMode.List,
                            SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary
                        };
                        picker.FileTypeFilter.Clear();
                        picker.FileTypeFilter.Add("*");
                        var folder = await picker.PickSingleFolderAsync();

                        if (folder != null)
                        {
                            var folderToDownload = fileSelected;
                            MyLogger.Log($"Folder downloading...\r\nCloud: {folderToDownload.FullName}\r\nLocal: {folder.Path}");
                            var downloadedFolder = await fileSelected.DownloadFolderOnWindowsAsync(folder);

                            if (downloadedFolder == null)
                            {
                                MyLogger.Log($"Folder download canceled!\r\nCloud: {folderToDownload.FullName}\r\nLocal: {folder.Path}");
                            }
                            else
                            {
                                MyLogger.Log($"Folder download succeeded!\r\nCloud: {folderToDownload.FullName}\r\nDownloaded: {downloadedFolder.Path}");
                            }
                        }
                        MyLogger.Log("All done!");
                    }
                    else
                    {
                        var picker = new Windows.Storage.Pickers.FolderPicker()
                        {
                            ViewMode = Windows.Storage.Pickers.PickerViewMode.List,
                            SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary
                        };
                        picker.FileTypeFilter.Clear();
                        picker.FileTypeFilter.Add("*");
                        var folder = await picker.PickSingleFolderAsync();

                        if (folder != null)
                        {
                            var fileToDownload = fileSelected;
                            {
                                try
                                {
                                    var existedFile = await folder.GetFileAsync(fileToDownload.Name);

                                    MyLogger.Assert(existedFile != null);
                                    if (await MyLogger.Ask($"\"{fileToDownload.Name}\" already existed in \"{folder.Path}\", overwrite anyway?"))
                                    {
                                        await existedFile.DeleteAsync();
                                    }
                                    else
                                    {
                                        goto indexSkip;
                                    }
                                }
                                catch (FileNotFoundException)
                                {
                                    MyLogger.Log("File not found exception, YA!");
                                }
                            }
                            var localFile = await folder.CreateFileAsync(fileToDownload.Name);

                            MyLogger.Log($"File downloading...\r\nCloud: {fileToDownload.FullName}\r\nLocal: {localFile.Path}");
                            await fileSelected.DownloadFileOnWindowsAsync(localFile);

                            indexSkip :;
                        }
                        MyLogger.Log("All done!");
                    }
                    break;

                default:
                    await MyLogger.Alert($"File picker currently not supported on {Device.RuntimePlatform} devices.");

                    break;
                }
            }
        }