AngularJS File upload with directive -
i use angularjs directive file upload , use directive way:
<input type="file" file-model="myfile">
the directive looks this:
myapp.directive('filemodel', ['$parse', function ($parse) { return { restrict: 'a', link: function(scope, element, attrs) { var model = $parse(attrs.filemodel); var modelsetter = model.assign; element.bind('change', function(){ scope.$apply(function(){ modelsetter(scope, element[0].files[0]); }); }); } }; }]); myapp.service('fileuploadservice', ['$http', function ($http) { this.uploadfiletourl = function(file, uploadurl) { var fd = new formdata(); fd.append('file', file); $http.post(uploadurl, fd, { transformrequest: angular.identity, headers: {'content-type': undefined} }) .success(function(){ console.log('file upload successful'); }) .error(function() { console.log('file upload error'); }); } }]);
in controller:
$scope.uploadfile = function() { var file = $scope.myfile; if (typeof file != 'undefined' && file != null) { console.log('file ' + file); console.dir(file); var uploadurl = "/upload"; fileupload.uploadfiletourl(file, uploadurl); } };
the upload works fine. have 2 questions concerning improvements of upload:
how upload more 1 file - there possibility upload more 1 file directive
if file uploaded - how
- how upload more 1 file - there possibility upload more 1 file directive
you must set multiple
on input field able select multiple files. then, file variable array of several files(if several files chosen) instead of file
object.
- if file uploaded - how to
... ?
Comments
Post a Comment