首页 > 解决方案 > 使用流星从谷歌云存储桶下载图像文件到IOS本地存储

问题描述

我正在使用 Meteor 项目将图像从 iOS 设备上传到谷歌云,并将相同的图像下载到 iOS 设备。

上传图片时我没有遇到任何问题,它存储在谷歌存储桶中。我面临的问题是在下载图像时,我使用下面的代码下载服务​​器路径上的图像。

bucket.file(srcFilename).download(options);

我想在 iOS 设备上下载和存储图像。当我尝试使用 createReadStream 读取文件时,我的应用程序卡住了,没有任何进展(没有得到任何回调)。

  bucket.file(srcFilename).createReadStream()
  .on('error', function(err) {
    console.log("error");
  })
  .on('response', function(response) {
    // Server connected and responded with the specified status and
    console.log("response");
  })
  .on('end', function() {
    // The file is fully downloaded.
    console.log("The file is fully downloaded.");
  })

我希望在将图像下载到 iOS 设备时不会丢失任何内容。我看了看,但找不到任何其他选择来做同样的事情。

在这方面的任何帮助都非常感谢,因为我被困在这一点上。

标签: node.jsmeteorgoogle-cloud-storage

解决方案


我使用下面的代码从谷歌云获取文件并下载我转换为二进制格式的块。然后我使用这种二进制格式从客户端显示图像并存储在我的本地存储中。

var chunkNew = new Buffer('');

 bucket.file(srcFilename).createReadStream().on('data', function (chunk) {
    chunkNew = Buffer.concat([chunkNew,chunk]);
  })
  .on('end', function() {
    // The file is fully downloaded.
    callback(null, chunkNew.toString('base64'));
  })

更多信息可以在这个链接http://codewinds.com/blog/2013-08-04-nodejs-readable-streams.html中找到,它使用数据块将图像显示为数组缓冲区。


推荐阅读