mongodb - 使用 cURL 上传文件

标签 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'

你能帮我或举个例子吗?

谢谢。

问候。

最佳答案

我很幸运 Meteor Uploads包裹。它会创建一个您可以 POST 到的上传端点。

关于mongodb - 使用 cURL 上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29012834/

相关文章:

javascript - 使用 Mongodb 聚合如何将数字更改为月份名称

ruby - 这是在 Ruby 中保持连接池的首选方法吗

node.js - 图像文件下载或在浏览器中显示

mongodb - 发送分块文件以保存在 mongodb 中

javascript - MongoDB 从 shell 中删除 GridFS 对象

mongodb - 如何在 Ubuntu 12.04 中并行安装两个版本的 mongodb?

mongodb - 如何检索 []bson.M 类型的 map

authentication - 在 Meteor 中使用密码的 Accounts.registerLoginHandler

ubuntu - "Error: conflict: logging can' t 满足 "when running " meteor ”命令

javascript - Meteor - .html 中的 If 语句