我是angularjs的新手,我想用文件创建一个表单。如何在angularjs控制器上获取文件并将其传递给laravel?
答案 0 :(得分:0)
看一下ng-file-upload非常容易配置和使用,并且有一些服务器端代码(也是PHP)的例子。
如果您使用的是凉亭,请安装runnig:
bower install ng-file-upload --save
并将ngFileUpload
添加到您的模块中:
var app = angular.module('myApp', [
// other dependencies here
'ngFileUpload'
]);
HTML:
<div class="button" ngf-select ngf-change="upload($files)">Upload on file change</div>
控制器:
$scope.upload = function (files) {
if (files && files.length) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
Upload.upload({
url: 'upload/url', // change this with the url/route of your laravel action
fields: {'username': $scope.username}, // this is an example to pass additional fields to pass in the post
file: file
}).progress(function (evt) {
var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name);
}).success(function (data, status, headers, config) {
console.log('file ' + config.file.name + 'uploaded. Response: ' + data);
alert('file uploaded!');
});
}
}
};
请参阅demo fiddle