图片上传Ember JS和Sails JS

时间:2018-03-06 10:07:55

标签: javascript ember.js sails.js

我正在尝试将图像从前端“Ember JS”上传到“Sails JS”,

我创建了一个名为“upload”的API,并使用“skipper”和“gridfs”在Mongo数据库中上传图像。

我也在使用“kitematic”虚拟服务器奇怪的是我的代码工作直到它到达

Sending 500 ('server error') response:
{ err : 'connection to [192.168.99:27017] timed out' }

出于某种原因我得到了

  {{#file-upload name="file"
            accept="image/*"
            multiple=true
            onfileadd=(action "uploadImage")}}
     <a id="upload-image" tabindex=0>Add an Image.</a>
  {{/file-upload}}

我在Ember中使用'ember-file-upload',这是我的前端

template.hbs

 uploadImage(file) {
  console.log(file);
  file.upload('http://localhost:1338/uploads/file').then(response => {
    console.log(response);
    this.get('model.files').pushObject({ path : response.body.message });
  })
}

controller.js

module.exports = { 
    file : function(req, res) {
        console.log(req.body);
        req.file('file').upload({
        adapter : require('skipper-gridfs'),
        uri : 'mongodb://192.168.99.100:32769/admin/upload'
        }, function(err, uploadedFiles) {
        if (err) return res.negotiate(err);
        console.log(err);
        return res.json({ message : uploadedFiles[0].fd.split('assets/')[1] })
        })
    }
};

现在Sails js上传控制器

{{1}}

1 个答案:

答案 0 :(得分:1)

对于初学者,请查看您获得的错误:

Sending 500 ('server error') response:
{ err : 'connection to [192.168.99:27017] timed out' }

你有192.168.99:27017的连接,这与你设置的完全不同:

mongodb://192.168.99.100:32769/admin/upload

首先,我认为你的URI不对 - 这个/ admin / upload对我来说似乎不对。尝试/admin.upload,或者省略整个事情,例如:

mongodb://192.168.99.100:32769

还要确保无需用户名和密码即可连接到mongoDB。

供参考检查此网址:http://api.mongodb.com/java/current/com/mongodb/MongoClientURI.html