使用cURL上传文件

时间:2015-03-12 14:45:09

标签: mongodb meteor gridfs

早上好,

我想知道是否有办法将文件从我的桌面上传到GridFS商店中的Meteor服务器。

这些是我为创建应用程序而安装的软件包:

accounts-base          1.1.3  A user account system
accounts-password      1.0.6  Password support for accounts
accounts-ui            1.1.4  Simple templates to add login widgets to an app
autopublish            1.0.2  Publish the entire database to all clients
cfs:gridfs             0.0.31  GridFS storage adapter for CollectionFS
cfs:standard-packages  0.5.4  Filesystem for Meteor, collectionFS
ian:bootstrap-3        3.3.1  HTML, CSS, and JS framework for developing responsive, mobile first projects on the web.
insecure               1.0.2  Allow all database writes by default
iron:router            1.0.7  Routing specifically designed for Meteor
meteor-platform        1.2.1  Include a standard set of Meteor packages in your app
nimble:restivus        0.6.2  Create authenticated REST APIs in Meteor 0.9.0+. Setup CRUD endpoints for Collections.

这是我的单文件代码:

var fileStore = new FS.Store.GridFS("fileStore");
Files = new FS.Collection("Files", {
  stores: [fileStore]
});

if (Meteor.isClient) {
  Template.demo.helpers({
    files: function() {
      var objs = Files.find();
      return Files.find();
    }
  });

  Template.demo.events({
    'change #file': function(e, t) {
      var file = t.find('#file').files[0];
      console.log(file.name);
      var newFile = new FS.File(file);
      newFile.metadata = {
        fn: "filename" 
      };
      Files.insert(newFile, function (err, fileObj) {
        if (!err) {
          var _fId = newFile._id;
          var _fn = file.name;
          var _collectionName = newFile.collectionName;
          var _basePath = "/cfs/files/";
          var _downloadPath = _basePath + _collectionName + "/" + _fId;

          FileList.insert({
            _id: _fId,
            fileName: _fn,
            collectionName: _collectionName,
            basePath: _basePath,
            downloadPath: _downloadPath
          });
        }
      });
    }
  });
}

if (Meteor.isServer) {
  Meteor.startup(function () {
    if (Meteor.users.find({}).count() === 0) { 
      Accounts.createUser({
        email : "user@name.it",
        password : "password",
        profile  : {
          //publicly visible fields like firstname goes here
        }
      });
    }

    // code to run on server at startup
    console.log("Set Restivus");
    Restivus.configure({
      useAuth: true,
      prettyJson: true
    });

    Restivus.addCollection(FileList);

    Files.allow({
      insert: function(){
        return true;
      },
      update: function(){
        return true;
      },
      remove: function(){
        return true;
      },
      download: function(){
        return true;
      }
    });
  });
}

我尝试了命令:

curl -v -X POST 'http://localhost:3000/cfs/files/Files/' -F 'file=@"download.jpeg";type=image/jpeg'

这是输出:

* Hostname was NOT found in DNS cache
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 3000 (#0)
> POST /cfs/files/Files/ HTTP/1.1
> User-Agent: curl/7.37.1
> Host: localhost:3000
> Accept: */*
> Content-Length: 5626
> Expect: 100-continue
> Content-Type: multipart/form-data; boundary=------------------------955a67d5c4b2f2c4
> 
< HTTP/1.1 100 Continue
< HTTP/1.1 501 Not Implemented
< vary: Accept-Encoding
< access-control-allow-origin: http://meteor.local
< access-control-allow-methods: PUT
< access-control-allow-headers: Content-Type
< date: Thu, 12 Mar 2015 14:25:55 GMT
< connection: keep-alive
< transfer-encoding: chunked
* HTTP error before end of send, stop sending
< 
* Closing connection 0

通过cURL读取文件的命令可以正常工作:

curl -v -X GET 'http://127.0.0.1:3000/cfs/files/Files/d6DAowcAwZcXTKnJ5'

你能帮助我或提供一个例子吗?

谢谢。

问候。

1 个答案:

答案 0 :(得分:1)

我对Meteor Uploads套餐感到满意。它创建了一个可以POST到的上传端点。