0

我正在尝试使用 Dropbox 进行上传,但出了点问题,我不知道它是什么。我在互联网上进行了一些搜索,但找不到任何东西。

这是我的代码:

if (Meteor.isClient) {
  Template.hello.helpers({
    uploads:function(){
      return Avatars.find();
    },
    images:function(){
      return Images.find();
    }
  });

  var avatarStoreLarge = new FS.Store.Dropbox("avatarsLarge");
  var avatarStoreSmall = new FS.Store.Dropbox("avatarsSmall");

  Avatars = new FS.Collection("avatars", {
    stores: [avatarStoreSmall, avatarStoreLarge],
    filter: {
      allow: {
        contentTypes: ['image/*']
      }
    }
  });

  Template.hello.events({
   'change .fileInput':function(event,template){
     FS.Utility.eachFile(event,function(file){
       var fileObj = new FS.File(file);
       Avatars.insert(fileObj,function(err){
         console.log(err);
       })
     })
   }
  });
}

if (Meteor.isServer) {
  Meteor.startup(function () {
    var avatarStoreLarge = new FS.Store.Dropbox("avatarsLarge", {
      key: "xxxxxxxxxxxxxxx", 
      secret: "xxxxxxxxxxxxxxx", 
      token: "xxxxxxxxxxxxxxx", 
      transformWrite: function(fileObj, readStream, writeStream) {
        gm(readStream, fileObj.name()).resize('250', '250').stream().pipe(writeStream)
      }
    })

    var avatarStoreSmall = new FS.Store.Dropbox("avatarsSmall", {
      key: "xxxxxxxxxxxxxxx", 
      secret: "xxxxxxxxxxxxxxx", 
      token: "xxxxxxxxxxxxxxx",
      beforeWrite: function(fileObj) {
        fileObj.size(20, {store: "avatarStoreSmall", save: false});
      },
      transformWrite: function(fileObj, readStream, writeStream) {
        gm(readStream, fileObj.name()).resize('20', '20').stream().pipe(writeStream)
      }
    })

    Avatars = new FS.Collection("avatars", {
      stores: [avatarStoreSmall, avatarStoreLarge],
      filter: {
        allow: {
          contentTypes: ['image/*']
        }
      }
    })
  });
}

我按照这个文档做了这个例子。

4

2 回答 2

1

首先,检查已安装的流星包:meteor list,要使代码正常工作,您必须安装:

cfs:standard-packages
cfs:dropbox

其次,简化代码以减少错误前兆(文档中的示例需要图像处理包),例如:

CLIENT-SIDE

var dataStorage = new FS.Store.Dropbox("imageData");

Images = new FS.Collection("imageData", {
    stores: [dataStorage],
    filter: {
        allow: {
            contentTypes: ['image/*']
            }
        }
    });

Template.helpers 块

'imageToShow': function(){
    return Images.find()
    },

Template.events 块

'click #imageToUpload': function(event, template) {
            FS.Utility.eachFile(event, function(file) {
                Images.insert(file, function (err, fileObj) {});
                });
            },

服务器端

var dataStorage = new FS.Store.Dropbox("imageData", {
    key: "...",
    secret: "...",
    token: "..."
    });

Images = new FS.Collection("imageData", {
    stores: [dataStorage],
    filter: {
        allow: {
            contentTypes: ['image/*']
            }
        }
    });

第三,处理错误以找出问题所在。

第四,从 Dropbox 上传和下载图片需要时间,以测试您的应用使用轻量级文件。console.log在客户端和服务器端使用以查看应用程序的流程。

于 2016-04-19T12:26:19.963 回答
0

首先检查您是否安装了以下软件包:

cfs:standard-packages
cfs:dropbox
cfs:gridfs

安装它们:

meteor add cfs:standard-packages
meteor add cfs:dropbox
meteor add cfs:gridfs

确保您的文件“client/collections_client/avatar.js”如下所示:

Template.hello.helpers({
  'imageToShow': function(){
      return Images.find()
      },
});

var dataStorage = new FS.Store.Dropbox("imageData");

Images = new FS.Collection("imageData", {
    stores: [dataStorage],
    filter: {
        allow: {
            contentTypes: ['image/*']
            }
        }
    });

Template.hello.events({
  'click #imageToUpload': function(event, template) {
             FS.Utility.eachFile(event, function(file) {
                 Images.insert(file, function (err, fileObj) {});
                 });
             },
});

“客户端/collections_client/hello.html”

<template name="hello">
  <input type="file" name="teste" id="imageToUpload">
</template>

<body>
{{> hello}}
</body>

假设您已经创建了您的应用程序

“服务器/collections_server/avatar.js”

var dataStorage = new FS.Store.Dropbox("imageData", {
    key: "yourAppKey",
    secret: "yourAppSecret",
    token: "youroAuth2Token"
    });

Images = new FS.Collection("imageData", {
    stores: [dataStorage]
    });
于 2016-07-22T11:17:29.733 回答