angularjs上传图片和文件
很简单:
$scope.upload = function() { var f = $scope.myFile; var fd = new FormData(); var img = document.querySelector('input[type=file]').files[0]; //var file = []; //file.push(f); // file.push(img); fd.append('issueId', '1'); fd.append('file', img); $http.post('/images', fd, { transformRequest: angular.identity, headers: { 'Content-Type': undefined } }).success(function (response) { debugger; }).error(function(response) { debugger; }); };
html
<div class="container" style="boeder: 1px solid #ddd"> <input type="file" name="pic" id="pic" emms-file-upload="myFile"> <a class="btn btn-primary" ng-click="upload()">upload</a> </div>
directives: 可用可不用
emmsMobile.directive('emmsFileUpload', function() { return { restrict: 'ACE', link: function(scope, elem, attr) { elem.on('change', function() { scope.file = this.files[0]; }) }, scope: { file: '=emmsFileUpload' } } })