【问题标题】:npm azure-storage download file as base64npm azure-storage 下载文件为 base64
【发布时间】:2018-06-13 12:15:50
【问题描述】:

我正在使用此package 将文件从 azure 下载到本地节点服务器。但是我不想将文件保存在本地服务器然后读取它以转换为base64,我想直接将其转换为base64。我怎样才能做到这一点?

 this.blobService.getBlobToStream('pictures', path, fs.createWriteStream(`./src/temp/${filename}.jpeg`), function(error, result, response) {
                if (!error) {
                    resolve({ message: `Items in container pictures:`, data: result});
                } else {
                    reject(error);
                }
            });

【问题讨论】:

    标签: node.js azure azure-blob-storage


    【解决方案1】:

    内置Node只支持ecnodeBuffer类型为base64,由于你不想下载blob到本地服务器,你必须使用外部模块编码stream

    npm install base64-stream 然后使用下面的代码。

    var base64 = require('base64-stream');
    ...
    this.blobService.createReadStream(container, path).pipe(base64.encode()).pipe(res);
    

    请注意,在您的 later question 中,您使用

    指定 Content-Type
    res.header('Content-Type', properties['contentType']);
    

    Content-Type 设置应省略。在我的测试中,如果成功设置Content-Type,图片将作为附件下载,而不是显示为内联内容。您的代码成功了,因为它实际上将Content-Type 设置为undefined

    您使用的属性是带有 blob 信息的 BlobResult(Json 格式)

    BlobResult {
      container: xxx,
      ...
      contentSettings:
       { contentType: 'image/jpg',
         contentMD5: 'uNkt2WJ75X13TjQbfY7qkA==' },
      ... 
    }
    

    所以contentType应该用这种格式properties.contentSettings.contentType

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-04
      • 1970-01-01
      • 2020-10-05
      • 2018-07-23
      • 2019-12-03
      • 2021-04-24
      • 2019-04-26
      • 2021-09-16
      相关资源
      最近更新 更多