node.js - 使用流星从谷歌云存储桶下载图像文件到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 设备时不会丢失任何内容。我看了看,但找不到任何其他选择来做同样的事情。
在这方面的任何帮助都非常感谢,因为我被困在这一点上。
解决方案
我使用下面的代码从谷歌云获取文件并下载我转换为二进制格式的块。然后我使用这种二进制格式从客户端显示图像并存储在我的本地存储中。
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中找到,它使用数据块将图像显示为数组缓冲区。
推荐阅读
- node.js - Notejs:如何将传入的网络数据包转换为流对象?
- css - Angular Material Table 在渲染超过 20 列时折叠
- mysql - 配置 c3p0 时如何修复 NoClassDefFoundError
- python - 使用列名的辅助列表访问 Django 查询集的值
- json - 我可以在发布之前获得 http.NewRequest 的大小吗?
- time - 将wireshark拆分为毫秒
- javascript - 在 Android 上全屏时 flv.js 媒体错误
- proxy - 访问代理后面的 ftp 服务器
- c# - 如何在 C# 中使用 tcp 发送字符串数组
- java - 在这种情况下使用什么来保存变量的值