Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
N

ng-file-upload

> 编程语言
Open source

Lightweight Angular directive to upload files with optional FileAPI shim for cross browser support

7.8K stars0 likes2 views
WebsiteGitHub

About

Lightweight Angular directive to upload files with optional FileAPI shim for cross browser support


ng-file-upload ===================

Lightweight Angular directive to upload files.

See the DEMO page. Reference docs here

Migration notes: version 3.0.x version 3.1.x version 3.2.x version 4.x.x version 5.x.x version 6.x.x version 6.2.x version 7.0.x version 7.2.x version 8.0.x version 9.0.x version 10.0.x version 11.0.x version 12.0.x version 12.1.x version 12.2.x

Ask questions on StackOverflow under the ng-file-upload tag.
For bug report or feature request please search through existing issues first then open a new one here. For faster response provide steps to reproduce/versions with a jsfiddle link. If you need support for your company contact me.
If you like this plugin give it a thumbs up at ngmodules or get me a cup of tea . Contributions are welcomed.

Table of Content:

  • Features
  • Install (Manual, Bower, NuGet, NPM)
  • Usage
  • Old Browsers
  • Server Side
    • Samples (Java, Spring, Node.js, Rails, PHP, .Net)
    • CORS
    • Amazon S3 Upload

Features

  • file upload progress, cancel/abort
  • file drag and drop (html5 only)
  • image paste from clipboard and drag and drop from browser pages (html5 only).
  • image resize and center crop (native) and user controlled crop through ngImgCrop. See crop sample (html5 only)
  • orientation fix for jpeg image files with exif orientation data
  • resumable uploads: pause/resume upload (html5 only)
  • native validation support for file type/size, image width/height/aspect ratio, video/audio duration, and ng-required with pluggable custom sync or async validations.
  • show thumbnail or preview of selected images/audio/videos
  • supports CORS and direct upload of file's binary data using Upload.$http()
  • plenty of sample server side code, available on nuget
  • on demand flash FileAPI shim loading no extra load for html5 browsers.
  • HTML5 FileReader.readAsDataURL shim for IE8-9
  • available on npm, bower, meteor, nuget

Install

  • Manual: download latest from here
  • Bower:
    • bower install ng-file-upload-shim --save(for non html5 suppport)
    • bower install ng-file-upload --save
  • NuGet: PM> Install-Package angular-file-upload (thanks to Georgios Diamantopoulos)
  • NPM: npm install ng-file-upload
xml
<script src="angular(.min).js"></script>
<script src="ng-file-upload-shim(.min).js"></script> 
<script src="ng-file-upload(.min).js"></script>

Usage

Samples:

  • Upload with form submit and validations: http://jsfiddle.net/danialfarid/maqbzv15/1118/
  • Upload multiple files one by one on file select: http://jsfiddle.net/danialfarid/2vq88rfs/136/
  • Upload multiple files in one request on file select (html5 only): http://jsfiddle.net/danialfarid/huhjo9jm/5/
  • Upload single file on file select: http://jsfiddle.net/danialfarid/0mz6ff9o/135/
  • Drop and upload with $watch: http://jsfiddle.net/danialfarid/s8kc7wg0/400/
  • Image Crop and Upload http://jsfiddle.net/danialfarid/xxo3sk41/590/
xml
<script src="angular.min.js"></script>

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

Upload on form submit or button click
<form ng-app="fileUpload" ng-controller="MyCtrl" name="form">
  Single Image with validations
  

  Multiple files
  

  Drop files: 

  <button type="submit" ng-click="submit()">submit</button>
</form>

Upload right away after file selection:

  Drop File:

Image thumbnail: 
Audio preview: <audio controls ngf-src="file"></audio>
Video preview: <video controls ngf-src="file"></video>

Javascript code:

…

Full reference

File select and drop

At least one of the ngf-select or ngf-drop are mandatory for the plugin to link to the element. ngf-select only attributes are marked with * and ngf-drop only attributes are marked with +.

xml

<a href="file | ngfDataUrl">image</a>

File preview

xml

  *ngf-background="file" 
  ngf-resize="{width: 20, height: 20, quality: 0.9}" 
  ngf-no-object-url="true or false" 
>

<div|span|...
 *ngf-thumbnail="file" 
 ngf-size="{width: 20, height: 20, quality: 0.9}" the image will be resized to this size
        
 ngf-as-background="boolean" 
>

Upload service:

…

ng-model The model value will be a single file instead of an array if all of the followings are true:

  • ngf-multiple is not set or is resolved to false.
  • multiple attribute is not set on the element
  • ngf-keep is not set or is resolved to false.

validation When any of the validation directives specified the form validation will take place and you can access the value of the validation using myForm.myFileInputName.$error.<validate error name> for example form.file.$error.pattern. If multiple file selection is allowed you can specify ngf-model-invalid="invalidFiles" to assing the invalid files to a model and find the error of each individual file with file.$error and description of it with file.$errorParam. You can use angular ngf-model-options to allow invalid files to be set to the ng-model ngf-model-options="{allowInvalid: true}".

Upload multiple files: Only for HTML5 FormData browsers (not IE8-9) you have an array of files or more than one file in your data to send them all in one request . Non-html5 browsers due to flash limitation will upload each file one by one in a separate request. You should iterate over the files and send them one by one for a cross browser solution.

drag and drop styling: For file drag and drop, ngf-drag-over-class could be used to style the drop zone. It can be a function that returns a class name based on the $event. Default is "dragover" string. Only in chrome It could be a json object {accept: 'a', 'reject': 'r', pattern: 'image/*', delay: 10} that specify the class name for the accepted or rejected drag overs. The pattern specified or ngf-pattern will be used to validate the file's mime-type since that is the only property of the file that is reported by the browser on drag. So you cannot validate the file name/extension, size or other validations on drag. There is also some limitation on some file types which are not reported by Chrome. delay default is 100, and is used to fix css3 transition issues from dragging over/out/over #277.

Upload.setDefaults(): If you have many file selects or drops you can set the default values for the directives by calling Upload.setDefaults(options). options would be a json object with directive names in camelcase and their default values.

Resumable Uploads: The plugin supports resumable uploads for large files. On your server you need to keep track of what files are being uploaded and how much of the file is uploaded.

  • url upload endpoint need to reassemble the file chunks by appending uploading content to the end of the file or correct chunk position if it already exists.
  • resumeSizeUrl server endpoint to return uploaded file size so far on the server to be able to resume the upload from where it is ended. It should return zero if the file has not been uploaded yet.
    A GET request will be made to that url for each upload to determine if part of the file is already uploaded or not. You need a unique way of identifying the file on the server so you can pass the file name or generated id for the file as a request parameter.
    By default it will assume that the response content is an integer or a json object with size integer property. If you return other formats from the endpoint you can specify resumeSizeResponseReader function to return the size value from the response. Alternatively instead of resumeSizeUrl you can use resumeSize function which returns a promise that resolves to the size of the uploaded file so far. Make sure when the file is fully uploaded without any error/abort this endpoint returns zero for the file size if you want to let the user to upload the same file again. Or optionally you could have a restart endpoint to set that back to zero to allow re-uploading the same file.
  • resumeChunkSize optionally you can specify this to upload the file in chunks to the server. This will allow uploading to GAE or other servers that have file size limitation and trying to upload the whole request before passing it for internal processing.
    If this option is set the requests will have the following extra fields: _chunkSize, _currentChunkSize, _chunkNumber (zero starting), and _totalSize to help the server to write the uploaded chunk to the correct position. Uploading in chunks could slow down the overall upload time specially if the chunk size is too small. When you provide resumeChunkSize option one of the resumeSizeUrl or resumeSize is mandatory to know how much of the file is uploaded so far.

Old browsers

For browsers not supporting HTML5 FormData (IE8, IE9, ...) FileAPI module is used. Note: You need Flash installed on your browser since FileAPI uses Flash to upload files.

These two files FileAPI.min.js, FileAPI.flash.swf will be loade

Issues· 328 open

View all issuesOpen on GitHub
  • #2160

    A

    Updated Nov 28, 2025
  • #2159

    ختمة

    Updated Sep 16, 2025
  • #2158

    Itinerario

    Updated Aug 9, 2025
  • #2153

    Test

    Updated Mar 23, 2025
  • #2152

    Abinash file

    Updated Jan 21, 2025
  • #2117

    support Angular2 or later

    Updated Nov 8, 2023
  • #1704

    Unable to send/receive custom metadata with S3 Upload

    Updated Dec 9, 2022
  • #1573

    ngf-fix-orientation creates invalid image file due to misinterpretation of exif metadata

    bugUpdated Dec 3, 2022
  • #2140

    Click on button with ngf-select no longer working in latest Chrome

    Updated Nov 30, 2022
  • #2150

    Missing width and height in the upload return object

    Updated May 6, 2022

> Tags

JavaScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedOct 1, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言