[英]AngularJS + SailsJS ng-file-upload
您能否向我推荐一个Sails.Js + Angular.js的示例,用于将文件上传到服务器,然后查看它们。 我已经审查了很多示例,但都不合适。 当我每次尝试下载控制台时出现错误时:
XMLHttpRequest无法加载localhost:1337 / analyzes / upload。 对预检请求的响应未通过访问控制检查:“ Access-Control-Allow-Origin”标头包含无效值“”。 因此,不允许访问Origin localhost:3000。
我的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);
});
};
我的HTML:
<div class="button" ngf-select="upload($file)">Upload on file select</div>
还有我的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
});
});
}
};
我觉得问题的根源是我使用单独的URL来显示网站(localhost:3000),而另一个用于Sails.Js(localhost:1337)。
我正在使用这个https://github.com/jakemmarsh/angularjs-gulp-browserify-boilerplate作为前端
为我的英语而哭。
你是对的; 问题与您的Sails应用有关,该应用不允许localhost:3000。 在http://sailsjs.org/documentation/reference/configuration/sails-config-cors上查看文档。
基本上,您需要通过CORS允许localhost:3000。 从上面的文档
'/foo/bar': {
target: 'FooController.bar',
cors: {
origin: 'http://foobar.com,https://owlhoot.com',
methods: 'GET,PUT,POST,OPTIONS,HEAD'
}
}
在您的情况下, origin
是http:// localhost:3000 。 同样不要忘记替换端点路由和target
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.