如何使用 Azure Blob 创建 HTML 链接来下载链接

发布于 2025-01-20 01:28:55 字数 750 浏览 0 评论 0原文

我正在使用Azure Blobs创建一个网站来存储内容。该网站提供搜索和索引。

当此链接是相对的时,

<a download="" href="./media5/yyy.png">Download</a>

浏览器将启动“下载”。

当文件存储在斑点中时,用户获得了一个链接:

<a download="" href="https://xxx.blob.core.windows.net/media5/yyy.png">Download</a>

但是,此导航到图像。

我需要浏览器“下载”才能工作。

我已经尝试设置存储帐户的CORS刀片:

但这没有做任何事情。

I am creating a website using Azure Blobs to store content. The website provides Search and Indexing.

When this link is relative,

<a download="" href="./media5/yyy.png">Download</a>

the browser kicks off a "download".

enter image description here

When the files are stored in Blobs, the users get a link like:

<a download="" href="https://xxx.blob.core.windows.net/media5/yyy.png">Download</a>

However, this navigates to the image.

I need the browser "download" to work.

I have tries setting the Storage Account's CORS Blade:
enter image description here

But this did not do anything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

潦草背影 2025-01-27 01:28:55

在这种情况下,CORS不会有帮助。如果要强制下载斑点,请将BLOB的内容类型属性更改为application/ocket-stream(或application/application/binary)。

但是,请注意,当您将BLOB的内容类型更改为application/Octet-Stream时,它将始终下载。您将无法在浏览器中显示斑点。

CORS is not going to help in this case. If you want to force download the blob, please change the blob's content-type property to application/octet-stream (or application/binary).

However, please note that when you change the blob's content type to application/octet-stream, it will always be downloaded. You will not be able to display the blob in the browser.

墟烟 2025-01-27 01:28:55

Manrti是正确的CORS没有发挥作用。我删除了存储帐户的CORS刀片。

修复是3个部分:
1:将ContentDispoistion标题设置为“附加”

   var blockBlobClient = new BlockBlobClient(connectionString, container, fileInfo.Name, clientOptions);

        var uploadOptions = new BlobUploadOptions();
        uploadOptions.HttpHeaders = new BlobHttpHeaders();

        switch (fileInfo.Extension)
        {
            case ".wav":
                uploadOptions.HttpHeaders.ContentType = "audio/wav";
                break;
            case ".mp3":
                uploadOptions.HttpHeaders.ContentType = "audio/mpeg";
                break;
            case ".mp4":
                uploadOptions.HttpHeaders.ContentType = "video/mp4";
                break;
            case ".jpg":
                uploadOptions.HttpHeaders.ContentType = "image/jpeg";
                break;
            case ".png":
                uploadOptions.HttpHeaders.ContentType = "image/png";
                break;
            case ".zip":
                uploadOptions.HttpHeaders.ContentType = "application/x-zip-compressed";
                break;
            case ".html":
                uploadOptions.HttpHeaders.ContentType = "text/plain";
                break;
            case ".pdf":
                uploadOptions.HttpHeaders.ContentType = "application/pdf";
                break;
            default:
                break;
        }

        uploadOptions.HttpHeaders.ContentDisposition = "attached";
        uploadOptions.ProgressHandler = progressHandler;

        var contentMD5 = await GetContentMD5(blockBlobClient);
        var contentType = await GetContentType(blockBlobClient);

        using (var fs = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            if ((sourceMD5 != contentMD5) | (contentType != uploadOptions.HttpHeaders.ContentType))
            {
                Console.WriteLine("\t\tUploading Blob...");
                await blockBlobClient.UploadAsync(fs, uploadOptions);
                contentMD5 = await GetContentMD5(blockBlobClient);
                if (sourceMD5 != contentMD5)
                {
                    throw new Exception($"Uploaded blob ContentMD5[{contentMD5}] does not match SourceMD5Hash[{sourceMD5}] for {blockBlobClient.Name}");
                }
            }
            return blockBlobClient;
        }

2:您必须使用SAS URL才能返回内容分配标题:

var uri = blobClient.GenerateSasUri(BlobSasPermissions.Read, new DateTimeOffset(DateTime.UtcNow.AddYears(2)));

3:HTML链接:

<a download="" href="https://xxx.blob.core.windows.net/media5/yyy.png?sv=2020-08-04&se=2024-04-08T14%3A43%3A28Z&sr=b&sp=r&sig=ZWdbpd8y2hr02MpzgxDC%2Fu2eqi5HukIYhXnLiYK4Rrk%3D">Download</a>

Manrti is correct CORS is not in play. I deleted the Storage Account's CORS Blade.

The fix is 3 parts:
1: Set ContentDispoistion header to "attached"

   var blockBlobClient = new BlockBlobClient(connectionString, container, fileInfo.Name, clientOptions);

        var uploadOptions = new BlobUploadOptions();
        uploadOptions.HttpHeaders = new BlobHttpHeaders();

        switch (fileInfo.Extension)
        {
            case ".wav":
                uploadOptions.HttpHeaders.ContentType = "audio/wav";
                break;
            case ".mp3":
                uploadOptions.HttpHeaders.ContentType = "audio/mpeg";
                break;
            case ".mp4":
                uploadOptions.HttpHeaders.ContentType = "video/mp4";
                break;
            case ".jpg":
                uploadOptions.HttpHeaders.ContentType = "image/jpeg";
                break;
            case ".png":
                uploadOptions.HttpHeaders.ContentType = "image/png";
                break;
            case ".zip":
                uploadOptions.HttpHeaders.ContentType = "application/x-zip-compressed";
                break;
            case ".html":
                uploadOptions.HttpHeaders.ContentType = "text/plain";
                break;
            case ".pdf":
                uploadOptions.HttpHeaders.ContentType = "application/pdf";
                break;
            default:
                break;
        }

        uploadOptions.HttpHeaders.ContentDisposition = "attached";
        uploadOptions.ProgressHandler = progressHandler;

        var contentMD5 = await GetContentMD5(blockBlobClient);
        var contentType = await GetContentType(blockBlobClient);

        using (var fs = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            if ((sourceMD5 != contentMD5) | (contentType != uploadOptions.HttpHeaders.ContentType))
            {
                Console.WriteLine("\t\tUploading Blob...");
                await blockBlobClient.UploadAsync(fs, uploadOptions);
                contentMD5 = await GetContentMD5(blockBlobClient);
                if (sourceMD5 != contentMD5)
                {
                    throw new Exception(
quot;Uploaded blob ContentMD5[{contentMD5}] does not match SourceMD5Hash[{sourceMD5}] for {blockBlobClient.Name}");
                }
            }
            return blockBlobClient;
        }

2: You must use a SAS Url to have the Content-Disposition header be returned:

var uri = blobClient.GenerateSasUri(BlobSasPermissions.Read, new DateTimeOffset(DateTime.UtcNow.AddYears(2)));

3: HTML Link:

<a download="" href="https://xxx.blob.core.windows.net/media5/yyy.png?sv=2020-08-04&se=2024-04-08T14%3A43%3A28Z&sr=b&sp=r&sig=ZWdbpd8y2hr02MpzgxDC%2Fu2eqi5HukIYhXnLiYK4Rrk%3D">Download</a>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文