ngUpload

An AngularJS file upload directive.

0.5.5 - for updates see CHANGELOG.md

   <form ng-upload="uploadComplete(content)" action="/upload-full-form">
       <p>
           <label>Fullname:</label>
           <input type="text" name="fullname" ng-model="fullname" />
       </p>
       <p>
           <label>Gender:</label>
           <input type="text" name="gender" ng-model="gender" />
       </p>
       <p>
           <label>Favorite Color:</label>
           <input type="text" name="color" ng-model="color"/>
       </p>
       <p>
           <label>Your picture (file will not be saved on the server):</label>
           <input type="file" name="file" />
       </p>
       <p>
           <input type="submit" class="btn" value="Submit" ng-disabled="$isLoading"  />
       </p>
   </form>
   <div class="alert alert-info">Server Response: {{response | json}}</div>
   <div>
       Fullname: <b>{{response.fullname}}</b><br />
       Gender: <b>{{response.gender}}</b><br />
       Favourite Color: <span ng-style="response.style">{{response.color}}</span><br />
       Picture: {{response.pictureUrl}}
   </div>

... and the context ngController's source is:

app.controller('Example5Ctrl', function ($scope) {
  $scope.uploadComplete = function (content) {
    $scope.response = JSON.parse(content); // Presumed content is a json string!
    $scope.response.style = {
      color: $scope.response.color,
      "font-weight": "bold"
    };

    // Clear form (reason for using the 'ng-model' directive on the input elements)
    $scope.fullname = '';
    $scope.gender = '';
    $scope.color = '';
    // Look for way to clear the input[type=file] element
  };
});

Requirements

Install with Bower

bower install ngUpload 

Usage

Add to your html file

<script src="/js/ng-upload.js"></script>

Create a basic form with a file input element

<div ng-app="app">
  <div ng-controller="mainCtrl">
   <form action="/uploads" ng-upload="complete(content)"> 
     <input type="file" name="avatar"></input>
     <input type="submit" value="Upload" ng-disabled="$isLoading"></input>
   </form>
 </div>
</div>

Some rule of thumb

Applying this rules, the sample above can be re-written as

<div ng-app="app">
  <div ng-controller="mainCtrl">
   <form action="/uploads" ng-upload="completed(content)"> 
     <input type="file" name="avatar"></input>
     <div style='cursor: pointer' upload-submit>Upload with Div</div> &bull;
   </form>
 </div>
</div>

or

<div ng-app="app">
  <div ng-controller="mainCtrl">
   <form action="/uploads" ng-upload="complete(contents)"> 
     <input type="file" name="avatar"></input>
     <a href='javascript:void(0)' 
       class="upload-submit" >
         Upload with Anchor</a>
   </form>
 </div>
</div>

where the form can be submit with a Div or Anchor html element.

The AngularJS controller for the above samples is given as:

angular.module('app', ['ngUpload'])
  .controller('mainCtrl', function($scope) {
    $scope.complete = function(content) {
      console.log(content); // process content
    }
});

In order, for ngUpload to respond correctly for IE, your server needs to return the response back as html/text not application/json

Directive Options

ngUpload

uploadSubmit

Example

Example of forms that posts to NodeJS server are now included under the /examples folder.

Test

Needs Chrome Installed.

npm install
npm install grunt-cli -g

npm test

jshint

npm install
npm install grunt-cli -g

grunt jshint

Minify

npm install
npm install grunt-cli -g

grunt uglify

License

MIT

How to contribute

pull requests welcome.

please use the following style guidelines

(http://nodeguide.com/style.html)

Contributors

Thanks