[meteor][0.6.*] 用meteorjs,如何用Meteor.http下载文件? [英] [meteor][0.6.*] With meteorjs, how to download file with Meteor.http?

查看:39
本文介绍了[meteor][0.6.*] 用meteorjs,如何用Meteor.http下载文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的流星应用程序中,服务器尝试下载一些文件以将它们存储在文件系统中.我使用 Meteor.http 包来做到这一点,但实际上,如果下载文件,它们似乎已损坏.

In my meteor app, the server try to download some file to store them on filesystem. I use Meteor.http package to do that, but in fact, if file are downloaded, they seems to be corrupted.

var fileUrl = 'http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=5'; //for example
Meteor.http.call("GET", fileUrl, function funcStoreFile(error, result) {
    "use strict";
    if (!error) {
        var fstream = Npm.require('fs'),
            filename = './.meteor/public/storage/' + collectionId;

        fstream.writeFile(filename, result.content, function funcStoreFileWriteFS(err) {
            if (!err) {
                var Fiber = Npm.require('fibers');
                Fiber(function funcStoreImageSaveDb() {
                    MyfileCollection.update({_id: collectionId}, {$set: {fileFsPath: filename}});
                }).run();
            } else {
                console.log('error during writing file', err);
            }
        });
    } else {
        console.log('dl file FAIL');
    }
});

我做了一个从 public/storage 到 ../.meteor/public/storage 的符号链接,以启用从 url 直接下载 (http://localhost:3000/storage/myfileId)

I did a symlink from public/storage to ../.meteor/public/storage to enable direct download from url (http://localhost:3000/storage/myfileId)

当我比较用这个系统下载的文件和直接从浏览器下载的同一个文件时,它们是不同的.我的概念有什么问题?

When i compare the file downloaded with this system and the same file downloaded directly from a browser, they are different. What's wrong with my conception?

推荐答案

我遇到了类似的问题,并基于这个讨论做了一个解决方案:https://github.com/meteor/meteor/issues/905

I had a similar problem and made a solution based on this discussion: on https://github.com/meteor/meteor/issues/905

通过使用请求库,流星也在后台使用,可以避免二进制下载的问题.此外,我建议不要将小文件保存到文件系统,而是直接在 mongodb 中进行 base64 编码.如果您计划部署到meteor.com 或其他云服务,这是最简单的解决方案.我在开发过程中将文件保存到公共目录时发现的另一个故障是,meteor 正在为公共目录中的每个更改重新加载文件.这可能会导致数据损坏,因为正在下载文件的块.这是我根据上述讨论使用的一些代码.

By using the request library, which meteor is using under the hood as well, one can avoid the problem with binary downloads. Besides I would recommend not saving small files to the filesystem but base64 encoded in mongodb directly. This is the easiest solution, if you plan to deploy to meteor.com or other cloud services. An other glitch I found when saving files to the public dir in development is that meteor is reloading the files for every change in the public dir. this can lead to data corruption as chunks of the file are being downloaded. Here some code i am using based on the above discussion.

Future = Npm.require("fibers/future")
request = Npm.require 'request'    
Meteor.methods
      downloadImage: (url) ->
        if url
          fut = new Future()
          options =
              url: url
              encoding: null
          # Get raw image binaries
          request.get options, (error, result, body) ->
              if error then return console.error error
              base64prefix = "data:" + result.headers["content-type"] + ";base64,"
              image = base64prefix + body.toString("base64")
              fut.ret image
          # pause until binaries are fully loaded
          return fut.wait()
        else false

Meteor.call 'downloadImage', url, (err, res) ->
  if res
    Movies.update({_id: id}, {$set: {image: res}})

希望对您有所帮助.

这篇关于[meteor][0.6.*] 用meteorjs,如何用Meteor.http下载文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆