/// <summary> /// Download the file to a local file. /// </summary> /// <param name="blob"></param> /// <param name="fileCreatedReceiver">Object that will receive a notification once the file is created.</param> public async Task DownloadFileFromBlob(CloudBlob blob, IFileCreated fileCreatedReceiver = null) { CloudBlockBlob blockBlob = await GetBlockBlob(blob); string filename = App.FileManager.CreateNewFilenameForDownloads(blob.Name); using (var fileStream = File.OpenWrite(filename)) { await blockBlob.DownloadToStreamAsync(fileStream); } fileCreatedReceiver?.FileCreated(filename); }
/// <summary> /// Download the blob and return the string value. /// If the Content Type isn't that of text, an empty string will be returned. /// </summary> /// <param name="blob"></param> /// <param name="saveToTempFile">Set this to TRUE if you want the contents of the text to be saved in a temp-file.</param> /// <param name="fileCreatedReceiver">Object that will receive a notification once the file is created. This is only active is "saveToTempFile" is set to TRUE.</param> /// <returns></returns> public async Task <String> DownloadTextContentFromBlob(CloudBlob blob, bool saveToTempFile = false, IFileCreated fileCreatedReceiver = null) { if (!blob.Properties.ContentType.Equals(Constants.BlobTextContentType)) { return(String.Empty); } CloudBlockBlob blockBlob = await GetBlockBlob(blob); string text = String.Empty; using (var memoryStream = new MemoryStream()) { await blockBlob.DownloadToStreamAsync(memoryStream); text = Encoding.UTF8.GetString(memoryStream.ToArray()); } if (saveToTempFile) { App.FileManager.CreateTempFile(); using (var file = new StreamWriter(App.FileManager.TempTxtFile)) { await file.WriteLineAsync(text); } fileCreatedReceiver?.FileCreated(App.FileManager.TempTxtFile); } return(text); }