Note that there are some explanatory texts on larger screens.

plurals
  1. POhow can we upload a file in angularJS
    text
    copied!<p>is there any direct approach for file upload in angularJS. I am completely new to angularJS. Tried googling about this,but couldn't find any basic example on this.do wee need to install any libraries/.js files </p> <pre><code> &lt;!DOCTYPE html&gt; &lt;html ng-app xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h5&gt;Upload&lt;/h5&gt; &lt;!--&lt;div class="span12"&gt;--&gt; &lt;div class="span4"&gt; &lt;div class="fileupload fileupload-new" data-provides="fileupload"&gt; &lt;div class="input-append"&gt; &lt;div class="uneditable-input span3"&gt; &lt;i class="icon-file fileupload-exists"&gt;&lt;/i&gt; &lt;span class="fileupload-preview" ng-model="userType"&gt;&lt;/span&gt; &lt;/div&gt; &lt;span class="btn btn-file"&gt; &lt;span class="fileupload-new"&gt;Select file&lt;/span&gt; &lt;span class="fileupload-exists"&gt;Change&lt;/span&gt; &lt;!--&lt;input type="file" name="file" /&gt;--&gt; &lt;input type="file" ng-model="upFile" onchange="angular.element(this).scope().setFileEventListener(this)" /&gt; &lt;/span&gt; &lt;a href="#" class="btn fileupload-exists" data-dismiss="fileupload"&gt;Remove&lt;/a&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div class="span2" &gt; &lt;button type="submit" value="Send" id="btnStartUpload" class="btn start" ng-click="uploadFile()" ng-disabled="!upload_button_state"&gt; &lt;i class="icon-upload"&gt;&lt;/i&gt; &lt;span&gt;Upload File&lt;/span&gt; &lt;/button&gt; &lt;/div&gt; &lt;script&gt; $scope.setFileEventListener = function (element) { $scope.uploadedFile = element.files[0]; if ($scope.uploadedFile) { $scope.$apply(function () { $scope.upload_button_state = true; }); } } $scope.uploadFile = function () { uploadFile(); }; function uploadFile() { if (!$scope.uploadedFile) { return; } ajax_post.uploadFile_init($scope.uploadedFile) .then(function (result) { if (result.status == 200) { $scope.storeDB_button_state = true; clientInfo.imagePath = "/uploadsfolder/" + $scope.uploadedFile.name; } }, function (error) { alert(error.message); }); } &lt;/script&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload