Travis Pettry
Travis Pettry

Reputation: 1352

Blazor Download Azure Blob from URL

I have a Blazor "download button" component that will get a URI from my API that contains the absolute URI of a Blob in Azure Blob Storage. Once the URI is returned from the API I call a JS function to create the a href you see blow. When the a href is clicked on the file is opened instead of being downloaded. Does anyone know what I am doing wrong?

JS code: I am using JS isolation in my component

export function Download(url, fileName) {
    let a = document.createElement('a');
    a.href = url;
    a.download = fileName;
    document.body.appendChild(a);
}

Resulting a href

<a href="https://myBlobStorage.blob.core.windows.net/filePath.txt?sv=sharedAccessToken" download="FileName.txt"></a>

Thank you,
Travis Pettry

Upvotes: 0

Views: 1957

Answers (1)

Travis Pettry
Travis Pettry

Reputation: 1352

I ended up using a Blazor library to download the file stream from the server.

Library: https://github.com/arivera12/BlazorDownloadFile

I changed my server to return a File response:

return File(blob.DownloadStreaming().Value.Content, blob.GetProperties().Value.ContentType);

Then in the Blazor I invoke the download code:

var resp = await _httpClient.GetAsync($"URL");
await BlazorDownloadFileService.DownloadFile("test.txt", await resp.Content.ReadAsByteArrayAsync(), resp.Content.Headers.ContentType.ToString());

Upvotes: 2

Related Questions