Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs file upload

html

    <form method='POST' enctype='multipart/form-data' name="formName">
        <div class="row">
            <div class="col-md-6">
                <input type="file" style="text-align: left" ng-model="value" class="btn"/>   
            </div>
            <div class="col-md-2">
                <input type="submit" value="upload" class="btn btn-info" ng-click="submitFile()"/>
            </div>
        </div>
    </form>

AngularJs

$scope.submitFile = function(){
    document.formName.action = 'http://xxx.xxx.xxx.xxx:8000/ww/up?s=' + $rootScope.reply.Sid; //$rootScope.reply.Sid is secession id
    document.formName.submit();
};  

I am trying to do a fileupload with AngularJs. Will this logic work?. My selected path is also coming as given below.

C:\fakepath\license.txt

Is this an error?

Note: Our UI team was able to the fileupload with the below code. I was trying to attain the same thing in AngularJs

<body>
    <form method='POST' enctype='multipart/form-data' action="http://xxx.xxx.xx.xxx:xxxx/yyy/yyyyyyyyy?s=3e3646ea-48cc-4342-a388-e0c0d7bbf4e4"/'>
    File to upload: <input type=file id='up_file' name=upfile><br>
</body>
like image 237
Jithin Shaji Avatar asked Nov 01 '22 03:11

Jithin Shaji


1 Answers

You did it right .. you only have to change few things to make it work

Change

<form method='POST' enctype='multipart/form-data' name="formName">

To

<form action="{{action}}" method='POST' enctype='multipart/form-data' name="formName">

In controller inject $timeout along with $scope

app.controller('Test', function($scope, $rootScope, $timeout){

 $scope.submitFile = function(){
  $scope.action = 'http://xxx.xxx.xxx.xxx:8000/ww/up?s=' + $rootScope.reply.Sid;

  $timeout(function(){
   document.formName.submit();
  }, 100);

 }

});

Action assigning $scope.action with new data .. angularjs needs to update the dom .. that is the reason we are using $timeout and submitting the form

like image 159
Mr.MAD Avatar answered Nov 11 '22 12:11

Mr.MAD