You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过Blazor调用JS方法下载Azure Blob存储文件后文件损坏的问题求助

通过Blazor调用JS方法下载Azure Blob存储文件后文件损坏的问题求助

我在Blazor Web应用里写了个方法,用来从API获取文件的URL——这些文件已经上传到Azure Blob存储了。如果直接用API返回的URL下载,文件能正常打开,一点问题都没有。

但用我自己写的方法下载时,虽然显示下载成功,可文件就是打不开,看起来像是损坏了。

我的Blazor方法:

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;
}

我调用的JavaScript方法:

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);
}

有没有大佬能帮我看看,我到底漏掉了什么?

备注:内容来源于stack exchange,提问作者Leo Ramadani

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 10:19:37