ng-file-upload教程

一、ng-file-upload简介
  是一款轻量级、跨浏览器的angular上传文件指令
二、特点
  (1)支持文件上传进度条、取消、暂停
  (2)支持文件拖放和黏贴图像
  (3)支持暂停和取消文件上传
  (4)支持验证文件的类型 / 大小、图像宽度 / 高度、视频 / 音频持续时间(ng-required)
  (5)支持预览显示选择的图像、视频、音频
  (6)支持CORS和直接上传文件的二进制数据( Upload.$http() )
  .....
三、用法
  示例:
html:

  
   
   
   

  
watching model:
Upload using model $watch
Upload multiple images using model $watch
Upload on file change
Upload multiple with size limitation
Drop File:
Drop Images or PDFs files here
File Drag/Drop is not supported for this browser
Image thumbnail: ![](file || '/thumb.jpg') Audio preview: Video preview:

JS:

//inject directives and services. 
var app = angular.module('fileUpload', ['ngFileUpload']); 
app.controller('MyCtrl', ['$scope', 'Upload', function ($scope, Upload) { 
    $scope.$watch('file', function (file) { 
        $scope.upload($scope.file); 
    }); 
/* optional: set default directive values */ 
//Upload.setDefaults( {ngf-keep:false ngf-accept:'image/*', ...} );
$scope.upload = function (file) { 
    Upload.upload(
        { 
            url: 'upload/url', 
            fields: {'username': $scope.username}, file: file 
        })
        .progress(function (evt) { 
            var progressPercentage = parseInt(100.0 * evt.loaded / evt.total); 
            console.log('progress: ' + progressPercentage + '% ' + evt.config.file.name); 
        })
        .success(function (data, status, headers, config) { 
            console.log('file ' + config.file.name + 'uploaded. Response: ' + data); 
        })
        .error(function (data, status, headers, config) { 
            console.log('error status: ' + status); 
        })
     }; 
}]);

涉及参数:
  (1)文件选择:----适用于:

你可能感兴趣的:(ng-file-upload教程)