简体   繁体   English

AngularJS + SailsJS ng文件上传

[英]AngularJS + SailsJS ng-file-upload

could you recommend me an example of Sails.Js + Angular.js for upload files to the server and then views them. 您能否向我推荐一个Sails.Js + Angular.js的示例,用于将文件上传到服务器,然后查看它们。 I've reviewed a lot of examples, but neither is suitable. 我已经审查了很多示例,但都不合适。 When i try to download every time i get error in console: 当我每次尝试下载控制台时出现错误时:

XMLHttpRequest cannot load localhost:1337/analyzes/upload. XMLHttpRequest无法加载localhost:1337 / analyzes / upload。 Response to preflight request doesn't pass access control check: The 'Access-Control-Allow-Origin' header contains the invalid value ''. 对预检请求的响应未通过访问控制检查:“ Access-Control-Allow-Origin”标头包含无效值“”。 Origin localhost:3000 is therefore not allowed access. 因此,不允许访问Origin localhost:3000。

part of my AngularJS controller (with ng-file-upload module): 我的AngularJS控制器的一部分(带有ng-file-upload模块):

    $scope.upload = function (file) {
    Upload.upload({
        url: AppSettings.serverUrl+'/analyzes/upload',
        data: {file: file}
    }).then(function (resp) {
        console.log('Success ' + resp.config.data.file.name + 'uploaded. Response: ' + resp.data);
    }, function (resp) {
        console.log('Error status: ' + resp.status);
    }, function (evt) {
        var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
        console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
    });
};

My HTML: 我的HTML:

<div class="button" ngf-select="upload($file)">Upload on file select</div>

And my Sails.JS controller (also i have empty model): module.exports = { 还有我的Sails.JS控制器(我也有空模型):module.exports = {

  upload: function  (req, res) {
    req.file('analyzes').upload(function (err, files) {

      if (err)
        return res.serverError(err);

      return res.json({
        message: files.length + ' file(s) uploaded successfully!',
        files: files
      });
    });
  }
};

I feel that the source of the problem is that I use a separate URL to display website (localhost:3000) and other for Sails.Js (localhost:1337). 我觉得问题的根源是我使用单独的URL来显示网站(localhost:3000),而另一个用于Sails.Js(localhost:1337)。

I'am using this https://github.com/jakemmarsh/angularjs-gulp-browserify-boilerplate for front end 我正在使用这个https://github.com/jakemmarsh/angularjs-gulp-browserify-boilerplate作为前端

And sory for my English. 为我的英语而哭。

You are correct; 你是对的; the problem has to do with your Sails app not allowing localhost:3000. 问题与您的Sails应用有关,该应用不允许localhost:3000。 Check out the documentation at http://sailsjs.org/documentation/reference/configuration/sails-config-cors . http://sailsjs.org/documentation/reference/configuration/sails-config-cors上查看文档。

Basically, you need to allow localhost:3000 through CORS. 基本上,您需要通过CORS允许localhost:3000。 From the above documentation 从上面的文档

'/foo/bar': {
  target: 'FooController.bar',
  cors: {
    origin: 'http://foobar.com,https://owlhoot.com',
    methods: 'GET,PUT,POST,OPTIONS,HEAD'
  }
}

where origin is http://localhost:3000 , in your case. 在您的情况下, originhttp:// localhost:3000 And also don't forget to replace the endpoint route and target . 同样不要忘记替换端点路由和target

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM