javascript - 如何让用户看到正在下载的 Azure blob 存储文件,而不是在 Angular 的网络选项卡本身中下载完整的流
问题描述
我有一个下载按钮,用户单击该按钮可从 Azure Blob 存储下载媒体文件。我希望它可以立即下载,并且用户可以看到下载进度,但是在请求完成之前它不会下载。
正如您在上图中所见,下载流大约需要 30 秒,然后用户才能看到下载的文件。请有人指导我实现下图所示的行为
我正在使用 file-saver.js 在 Angular 中保存文件
this.downloadMediaService.downloadAsset(assetId).then((httpResponse) => {
const {body: blob} = httpResponse;
this.mediaBlob = blob;
Filesaver.saveAs(blob, this.assetName);
});
}
解决方案
如果想知道从 Azure blob 下载文件时下载了多少字节,可以使用 sdk@azure/storage-blob
来实现。
例如
- 安装sdk
npm install @azure/storage-blob
npm install crypto-js --save
npm install @types/crypto-js --save-dev
- 代码
import {BlobServiceClient,AnonymousCredential,newPipeline } from '@azure/storage-blob';
import * as CryptoJS from 'crypto-js';
...
downlaod(){
// genereate access token
const accountname =" storage account name";
const key="storage account key";
const start = new Date(new Date().getTime() - (15 * 60 * 1000));
const end = new Date(new Date().getTime() + (30 * 60 * 1000));
const signedpermissions = 'rwdlac';
const signedservice = 'b';
const signedresourcetype = 'sco';
const signedexpiry = end.toISOString().substring(0, end.toISOString().lastIndexOf('.')) + 'Z';
const signedProtocol = 'https';
const signedversion = '2018-03-28';
const StringToSign =
accountname + '\n' +
signedpermissions + '\n' +
signedservice + '\n' +
signedresourcetype + '\n' +
'\n' +
signedexpiry + '\n' +
'\n' +
signedProtocol + '\n' +
signedversion + '\n';
var str =CryptoJS.HmacSHA256(StringToSign,CryptoJS.enc.Base64.parse(key));
var sig = CryptoJS.enc.Base64.stringify(str);
const sasToken =`sv=${(signedversion)}&ss=${(signedservice)}&srt=${(signedresourcetype)}&sp=${(signedpermissions)}&se=${encodeURIComponent(signedexpiry)}&spr=${(signedProtocol)}&sig=${encodeURIComponent(sig)}`;
const containerName="test";
const pipeline =newPipeline (new AnonymousCredential(),{
retryOptions: { maxTries: 4 }, // Retry options
userAgentOptions: { userAgentPrefix: "AdvancedSample V1.0.0" }, // Customized telemetry string
keepAliveOptions: {
// Keep alive is enabled by default, disable keep alive by setting false
enable: false
}
});
const blobServiceClient =new BlobServiceClient(`https://${accountname}.blob.core.windows.net?${sasToken}`,
pipeline )
const containerClient =blobServiceClient.getContainerClient(containerName)
const blob =containerClient.getBlockBlobClient("SampleVideo_1280x720_1mb.mp4")
const r= await blob.getProperties()
const totalSize = r.contentLength
blob.download(null,null,{onProgress: (ev) =>
console.log("You have download "+ ev.loadedBytes +" bytes\nThe process:"+ev.loadedBytes/totalSize ) }
).then(response => response.blobBody.then(b => {
console.log(b.size)
// save file
}))
}
推荐阅读
- json - please help me to loop model and get data from rest api
- macos - Responding to Exposé in MacOS
- json - ERROR: NoSuchMethodError: The method 'toDouble' was called on null. Receiver: null Tried calling: toDouble()
- python - why is this not running correctly? thoughts?
- excel - Looking to import filter criteria from an excel table
- javascript - 是否可以在不使用上下文或事件的情况下向 DialogFlow 代理发送额外数据
- visual-studio - 将对象从一个程序集转换为另一个 cli c++
- javascript - 将图像上传到数据库(React、Express、MongoDB)
- kubernetes - 自动缩放后 Docker Hub 上的 Azure AKS 机密中的 Kubernetes 丢失
- jquery - 光滑的滑块 onChange