1

我需要上传“即时”用户的图片。但我收到错误 503 服务不可用。

用户.js

Meteor.subscribe('userImages');

Template.userProfil.events({
  'change [name=userPhotoUpload]': function(event) {
    event.preventDefault();
    FS.Utility.eachFile(event, function(file) {
        var newFile = new FS.File(file);
        newFile.metadata = {
            createdBy:Meteor.userId(),
        }
        userImages.insert(newFile, function (err, fileObj) {
            if (err){
             // handle error
            } else {
                // handle success depending what you need to do
                var currentUserId = Meteor.userId();
                var imagesURL = {
                  "profile.userImg": '/cfs/files/userImages/' + fileObj._id + '/' + fileObj.name()
                };
                Meteor.users.update(currentUserId, {$set: imagesURL});//there I get url and

            }
        });
    });

  }
});

路由器.js

Router.route('/organizer', {
  name: 'userProfil',
  template: 'userProfil',
  data: function() {
      var currentUser = Meteor.userId();
      return Meteor.user({_id: currentUser});
  }
});

用户img.html

<img src="{{profile.userImg}}">

上传图片后我得到这个错误:

http://localhost:3000/cfs/files/userImages/wNjvF8uuN8j6fd8md/exampl2.jpg 503 (Service Unavailable)

但是这条路径是绝对正确的,并且在手动重新加载页面后它的工作。

我怎么解决这个问题?

4

1 回答 1

1

好的,我找到了一些解决方案,但我认为这不是正确的方法。也许有人有更好的决定?

改变:

用户.js

'change [name=userPhotoUpload]': function(event) {
    event.preventDefault();
    FS.Utility.eachFile(event, function(file) {
        var newFile = new FS.File(file);
        newFile.metadata = {
            createdBy:Meteor.userId(),
        }
        userImages.insert(newFile, function (err, fileObj) {
            if (err){
             // handle error
            } else {

                var currentUserId = Meteor.userId();
                var intervalHandle = Meteor.setInterval(function () {
                            console.log("Inside interval");

                            // changes here:

                            if (fileObj.hasStored("userImages")) {
                                 //checked if image was stored
                                var imagesURL = {
                                  "profile.userImg": '/cfs/files/userImages/' + fileObj._id + '/' + fileObj.name()
                                };
                                Meteor.users.update(currentUserId, {$set: imagesURL});
                                // if file has stored, stop interval
                                Meteor.clearInterval(intervalHandle);
                            }
                        }, 1000);
            }
        });
    });


}
于 2016-03-09T14:27:49.457 回答