Ejemplo n.º 1
0
        /// <summary>
        /// Saves the selected attachment to file
        /// </summary>
        private async Task <bool> SaveAttachment()
        {
            if (SelectedIndex != -1)
            {
                byte[] byteArray;

                List <MimeEntity> list = CurrentMessage.Attachments.ToList();
                var attachment         = list[SelectedIndex];

                var fileName = attachment.ContentDisposition?.FileName ?? attachment.ContentType.Name;

                //Converts the attachment to a byte array
                byteArray = HelperUtils.ConvertAttachmentToByteArray(attachment);

                if (byteArray == null)
                {
                    return(false);
                }

                var savePicker = new Windows.Storage.Pickers.FileSavePicker();
                savePicker.SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.Desktop;

                //Gets file extension
                int    index    = fileName.LastIndexOf(".");
                string fileType = fileName.Substring(index);

                savePicker.FileTypeChoices.Add(fileType.Substring(1), new List <string>()
                {
                    fileType
                });

                savePicker.DefaultFileExtension = fileType;
                savePicker.SuggestedFileName    = fileName;

                //Lets the user choose how to save the file
                StorageFile file = await savePicker.PickSaveFileAsync();

                if (file != null)
                {
                    Windows.Storage.CachedFileManager.DeferUpdates(file);

                    //Writes byte array to Storagefile
                    await Windows.Storage.FileIO.WriteBytesAsync(file, byteArray);

                    Windows.Storage.Provider.FileUpdateStatus status =
                        await Windows.Storage.CachedFileManager.CompleteUpdatesAsync(file);

                    if (status == Windows.Storage.Provider.FileUpdateStatus.Complete)
                    {
                        return(true);
                    }
                    else
                    {
                        Debug.WriteLine("File " + file.Name + " couldn't be saved");
                        return(false);
                    }
                }
                else
                {
                    Debug.WriteLine("Operation cancelled");
                    return(false);
                }
            }
            else
            {
                return(false);
            }
        }