admin管理员组文章数量:1291050
I have this method in my blazor web app which I use to get a files' url from my api. The files are already uploaded in AzureBlobStorage and when I try to download them directly from the URL that my API provides me, I can open them and they're all fine.
However, when I try it with my methods, the download is successful but the I cannot open them since they seem to be corrupted.
My blazor method:
public async Task DownloadStream(FileProperties file)
{
DisableButton = true;
try
{
var blobUrl = await _httpClient.GetBlobUrl(file.Id.ToString());
await _jsRuntime.InvokeVoidAsync("fileDownload", blobUrl, file.Name);
}
catch (Exception ex)
{
Console.WriteLine("ERROR: JS method missing... " + ex.Message);
}
DisableButton = false;
}
My Javascript method that I invoke in my method:
function fileDownload(base64String, fileName) {
console.log("url=", base64String);
const a = document.createElement("a");
a.href = base64String;
a.download = fileName;
a.target = "_blank"; // Open in new tab if needed
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
What am I missing here?
I have this method in my blazor web app which I use to get a files' url from my api. The files are already uploaded in AzureBlobStorage and when I try to download them directly from the URL that my API provides me, I can open them and they're all fine.
However, when I try it with my methods, the download is successful but the I cannot open them since they seem to be corrupted.
My blazor method:
public async Task DownloadStream(FileProperties file)
{
DisableButton = true;
try
{
var blobUrl = await _httpClient.GetBlobUrl(file.Id.ToString());
await _jsRuntime.InvokeVoidAsync("fileDownload", blobUrl, file.Name);
}
catch (Exception ex)
{
Console.WriteLine("ERROR: JS method missing... " + ex.Message);
}
DisableButton = false;
}
My Javascript method that I invoke in my method:
function fileDownload(base64String, fileName) {
console.log("url=", base64String);
const a = document.createElement("a");
a.href = base64String;
a.download = fileName;
a.target = "_blank"; // Open in new tab if needed
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
}
What am I missing here?
Share Improve this question edited Feb 14 at 9:20 Jason Pan 21.9k2 gold badges19 silver badges43 bronze badges asked Feb 13 at 16:15 Leo RamadaniLeo Ramadani 2191 silver badge11 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 0This is the code behind I used for a base64 image prototype. I actually abandoned it and went with a file download controller method. I found performance, size, and browser caching issues this way.
The orchestration service is returning the blob stream.
public partial class DocumentBase64Img : ComponentBase
{
private readonly IDocumentOrchestrationService documentOrchestrationService;
private string? base64;
public DocumentBase64Img(IDocumentOrchestrationService documentOrchestrationService)
{
this.documentOrchestrationService = documentOrchestrationService;
}
[Parameter]
public string FileName { get; set; } = default!;
[Parameter]
public int Width { get; set; } = default!;
protected override async Task OnInitializedAsync()
{
var streamResult = await documentOrchestrationService.GetDocumentStreamAsync("container-name", FileName);
if (streamResult.IsSuccess)
{
var stream = streamResult.Value;
// copy all bytes to a buffer
using (var memoryStream = new MemoryStream())
{
stream.Seek(0, SeekOrigin.Begin);
await stream.CopyToAsync(memoryStream).ConfigureAwait(false); // copy stream to memoryStream
byte[] buffer = memoryStream.ToArray();
var base64 = Convert.ToBase64String(buffer);
this.base64 = $"data:image/jpeg;base64,{base64}";
await InvokeAsync(StateHasChanged);
}
}
}
}
Note: .Net 9 Blazor allows injecting via the constructor...
<img src="@base64" width="@Width" />
本文标签: javascriptFile downloaded from BlobStorage are corruptedStack Overflow
版权声明:本文标题:javascript - File downloaded from BlobStorage are corrupted - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741518058a2383013.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
_httpClient.GetBlobUrl(file.Id.ToString())
return a URL or does it return a Base64 string? Your code is confusing because thefileDownload
js function expects a base64String as the first parameter but your C#DownloadStream
method appears to be downloading a url. – pnavk Commented Feb 13 at 23:07XMLHttpRequest
to download the file, and setresponseType = "blob"
, you can find my sample code infunction bb()
. – Jason Pan Commented Feb 14 at 9:20