使用动态创建的控制angularjs上传文件 [英] File upload using dynamically created control angularjs

查看:67
本文介绍了使用动态创建的控制angularjs上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在特定条件下创建一个文件上传控件,然后上传文件。

i多次尝试但文件上传不能使用angularjs文件上传指令动态创建文件控件



我的尝试:



i want to create a file upload control on specific condition and then upload file.
i tried many times but file upload not working on dynamically created file control using angularjs file upload directive

What I have tried:

<html>
   
   <head>
      <script src = "http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
   </head>
   
   <body ng-app = "myApp">
	
      <div ng-controller="myCtrl">
<div ng-if="enabled">
         <input type = "file" file-model = "myFile"/>
</div>
<button ng-click = "enabled = true">Enalbe</button>
         <button ng-click = "uploadFile()">upload me</button>
      </div>
      
      <script>
         var myApp = angular.module('myApp', []);
         
         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('fileUpload', ['$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(){
               })
            
               .error(function(){
               });
            }
         }]);
      
         myApp.controller('myCtrl', ['$scope', 'fileUpload', function($scope, fileUpload){
            $scope.uploadFile = function(){
               var file = $scope.myFile;
               debugger
               console.log('file is ' );
               console.dir(file);
               
               var uploadUrl = "/fileUpload";
               fileUpload.uploadFileToUrl(file, uploadUrl);
            };
         }]);
			
      </script>
      
   </body>
</html></html>

推荐答案

解析',函数


parse){
return {
restrict:' A'
链接: function (范围,元素,attrs){
var model =
parse) { return { restrict: 'A', link: function(scope, element, attrs) { var model =


parse(attrs.fileModel);
var modelSetter = model.assign;

element.bind(' change' function (){
scope。
parse(attrs.fileModel); var modelSetter = model.assign; element.bind('change', function(){ scope.


这篇关于使用动态创建的控制angularjs上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆