javascript - Azure 存储 blob 库将 blob 下载到本地计算机
问题描述
我正在关注本教程并尝试查看如何在本地计算机上下载 blob 文件
这是一个使用 JavaScript 的网络应用程序。
以下代码行运行良好,我可以在 console.log 中看到文件的内容,而不是在 console.log 中看到它,我希望用户能够将文件下载到他们的本地计算机。所以也许给一个路径或其他东西。
我已经评论了我需要帮助的代码。它运行但什么也不做。我没有看到要下载到本地的消息。
const downloadFiles = async () => {
if (fileList.selectedOptions.length > 0) {
//reportStatus("downloading files...");
for (const option of fileList.selectedOptions) {
console.log("option.text", option.text);
try {
const blobClient = containerClient.getBlobClient(option.text);
console.log("BlobClient",blobClient)
// Hoping this line of code would do the trick
var blobResponse = await blobClient.downloadToFile(option.text);
//////
const downloadBlockBlobResponse = await blobClient.download();
console.log("downloadBlockBlobResponse", downloadBlockBlobResponse);
const downloaded = await blobToString(await downloadBlockBlobResponse.blobBody);
console.log("Downloaded blob to string content", downloaded);
// [Browsers only] A helper method used to convert a browser Blob into string.
async function blobToString(blob) {
const fileReader = new FileReader();
return new Promise((resolve, reject) => {
fileReader.onloadend = (ev) => {
resolve(ev.target.result);
};
fileReader.onerror = reject;
fileReader.readAsText(blob);
});
}
// [Node.js only] A helper method used to read a Node.js readable stream into a Buffer
async function streamToBuffer(readableStream) {
return new Promise((resolve, reject) => {
const chunks = [];
readableStream.on("data", (data) => {
chunks.push(data instanceof Buffer ? data : Buffer.from(data));
});
readableStream.on("end", () => {
resolve(Buffer.concat(chunks));
});
readableStream.on("error", reject);
});
}
}
catch (error) {
reportStatus(error.message);
}
}
reportStatus("Done.");
listFiles();
} else {
reportStatus("No files selected.");
}
/*
//const containerClient = blobServiceClient.getContainerClient(containerName);
//console.log("containerClient", containerClient);
const blobName = // "MyTest1.csv";
const blobClient = containerClient.getBlobClient(blobName);
console.log("blobClient", blobClient);
try {
const downloadBlockBlobResponse = await blobClient.download();
console.log("downloadBlockBlobResponse", downloadBlockBlobResponse);
const downloaded = await blobToString(await downloadBlockBlobResponse.blobBody);
console.log("Downloaded blob content", downloaded);
// [Browsers only] A helper method used to convert a browser Blob into string.
async function blobToString(blob) {
const fileReader = new FileReader();
return new Promise((resolve, reject) => {
fileReader.onloadend = (ev) => {
resolve(ev.target.result);
};
fileReader.onerror = reject;
fileReader.readAsText(blob);
});
}
} catch (error) {
reportStatus(error.message);
}
*/
};
解决方案
该downloadToFile
方法只在node.js
运行时可用,不能用于下载文件到本地,可以参考downloadToFile。
您可以尝试以下方法下载文件:</p>
const downloadFiles = async () => {
if (fileList.selectedOptions.length > 0) {
reportStatus("downloading files...");
for (const option of fileList.selectedOptions) {
console.log("option.text", option.text);
try {
const blobClient = containerClient.getBlobClient(option.text);
console.log("BlobClient",blobClient);
const downloadBlockBlobResponse = await blobClient.download();
const url =window.URL.createObjectURL(await downloadBlockBlobResponse.blobBody);
downloadURI(url,option.text)
console.log("Downloaded blob to string content", url);
}
catch (error) {
reportStatus(error.message);
}
}
reportStatus("Done.");
listFiles();
} else {
reportStatus("No files selected.");
}
};
downloadButton.addEventListener("click", downloadFiles);
function downloadURI(uri, name)
{
var link = document.createElement("a");
link.download = name;
link.href = uri;
link.click();
};
推荐阅读
- installation - 如何在安装过程中设置 pgAdmin 4 主密码?
- reactjs - react 使用了错误的 cookie
- reactjs - 我的 git 在反应时没有被识别,我不知道为什么
- java - 如何在android中使用GraphView绘制实时数据
- loopbackjs - loopback4 通过命令行创建控制器而没有提示问题
- javascript - 地图标记未出现在网站上
- api - 在 Visual Studio 默认选项中添加 REST API 客户端
- unit-testing - 人们测试他们的 clojure.spec 规范是否常见?
- apparmor - 如何在不同命名空间的 ubuntu 20.04 下使用 snap?获得许可被拒绝错误
- windows - EnableDelayedExpansion、For 和带有特殊字符(如 ! % &