Last active
November 5, 2020 14:55
-
-
Save ukcoderj/087c6f08bb4f328e196138123d294f9e to your computer and use it in GitHub Desktop.
Download a blob held in azure to an angular client
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class DownloadFile { | |
public fileName: string; | |
public displayName: string; | |
public contentType: string; | |
public content: any; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable, EventEmitter } from '@angular/core'; | |
import { HttpClient } from '@angular/common/http'; | |
import { Observable, throwError, of } from 'rxjs'; | |
import { catchError, tap } from 'rxjs/operators'; | |
import { saveFile, saveAs } from 'file-saver'; // npm install file-saver | |
import { Base64 } from 'js-base64'; // npm install js-base64 | |
import { DownloadFile } from '../models/dowload/download-file'; | |
@Injectable({ | |
providedIn: 'root' | |
}) | |
export class DownloadService { | |
constructor(private http: HttpClient) { | |
} | |
saveFileLocally(data: DownloadFile, filename: string) { | |
let blob = null; | |
if (filename.includes('.csv')) { | |
// Add the BOM, so excel can open it even with chinese characters | |
var BOM = "\uFEFF"; | |
var csvData = BOM + data.content; | |
blob = new Blob([csvData], { type: "text/csv;charset=utf-8" }); | |
} else { | |
blob = new Blob([data.content], { type: data.contentType }); | |
} | |
saveAs(blob, filename); | |
} | |
dowloadFile(filename: string, displayFileName: string): void { | |
var url = "https://URL..." + "fileName=" + filename + "&displayFileName=" + displayFileName; | |
this.http.get(url) | |
.subscribe((response: DownloadFile) => { | |
// Use Base64.decode as angular will base64 the byte array. Don't use atob as this won't handle chinese characters | |
response.content = Base64.decode(response.content); | |
this.saveFileLocally(response, displayFileName) | |
}, | |
error => console.log('Error downloading the file.'), | |
() => console.info('OK') | |
); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class DownloadFile | |
{ | |
public DownloadFile() { } | |
public DownloadFile(string fileName) | |
{ | |
FileName = fileName; | |
} | |
public string FileName { get; set; } | |
public string DisplayName { get; set; } | |
public string ContentType { get; set; } | |
public byte[] Content { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Net; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Authorization; | |
using Microsoft.AspNetCore.Mvc; | |
using Microsoft.Extensions.Localization; | |
using Microsoft.Extensions.Logging; | |
namespace WEBSITE.Controllers.api | |
{ | |
[ApiVersion("1.0")] | |
[Authorize] | |
[ApiController] | |
public class DownloadFileController : ControllerBase | |
{ | |
ILogger<DownloadFileController> _logger; | |
IStorageHelper _storageHelper; | |
public DownloadFileController(ILogger<DownloadFileController> logger, | |
IStorageHelper storageHelper) | |
{ | |
_logger = logger; | |
_storageHelper =storageHelper; | |
} | |
[HttpGet, Route("download")] | |
public async Task<ActionResult<DownloadFile>> Download(string fileName, string displayFilename) | |
{ | |
DownloadFile file = await _storageHelper.GetDownloadFileByNameAsync("{virtual-folder-name}", fileName); | |
//System.IO.File.WriteAllBytes("C:/Temp/report.csv", file.Content); | |
return file; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Azure; | |
using Azure.Storage.Blobs; | |
using Azure.Storage.Blobs.Models; | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
using System.Linq; | |
using System.Threading.Tasks; | |
namespace WEBSITE.Helpers | |
{ | |
public interface IStorageHelper | |
{ | |
Task<DownloadFile> GetDownloadFileByNameAsync(string vFolderName, string fileName); | |
} | |
public class StorageHelper : IStorageHelper | |
{ | |
string _connectionString; | |
public StorageHelper() | |
{ | |
_connectionString = "SET" | |
} | |
public async Task<DownloadFile> GetDownloadFileByNameAsync(string vFolderName, string fileName) | |
{ | |
DownloadFile downloadFile = new DownloadFile(); | |
BlobServiceClient blobServiceClient = new BlobServiceClient(_connectionString); | |
BlobContainerClient blobContainer = blobServiceClient.GetBlobContainerClient("{Container-Name}"); | |
if(String.IsNullOrEmpty(vFolderName)){ | |
downloadFile.FileName = fileName; | |
} | |
else{ | |
downloadFile.FileName = vFolderName + "/" + fileName; | |
} | |
BlobClient blobClient = blobContainer.GetBlobClient(downloadFile.FileName); | |
BlobDownloadInfo download = await blobClient.DownloadAsync(); | |
var properties = await blobClient.GetPropertiesAsync(); | |
downloadFile.ContentType = properties.Value.ContentType; | |
// Read the file and create a new file in the mid-process/unzipped folder. | |
using (MemoryStream blobMemStream = new MemoryStream()) | |
{ | |
await download.Content.CopyToAsync(blobMemStream); | |
downloadFile.Content = blobMemStream.ToArray(); | |
} | |
return downloadFile; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment