Angular2 File Upload

Install

Install the components

npm install ng2-file-upload --save

github:

https://github.com/valor-software/ng2-file-upload/tree/master

demo:

http://valor-software.com/ng2-file-upload/

demo code

     <style>
.my-drop-zone { border: dotted 3px lightgray; }
.nv-file-over { border: dotted 3px red; } /* Default class applied to drop zones on over */
.another-file-over-class { border: dotted 3px green; } html, body { height: 100%; }
</style> <div class="container"> <div class="navbar navbar-default">
<div class="navbar-header">
<a class="navbar-brand" href>Angular2 File Upload</a>
</div>
</div> <div class="row"> <div class="col-md-3"> <h3>Select files</h3> <div ng2FileDrop
[ngClass]="{'nv-file-over': hasBaseDropZoneOver}"
(fileOver)="fileOverBase($event)"
[uploader]="uploader"
class="well my-drop-zone">
Base drop zone
</div> <div ng2FileDrop
[ngClass]="{'another-file-over-class': hasAnotherDropZoneOver}"
(fileOver)="fileOverAnother($event)"
[uploader]="uploader"
class="well my-drop-zone">
Another drop zone
</div> Multiple
<input type="file" ng2FileSelect [uploader]="uploader" multiple /><br/> Single
<input type="file" ng2FileSelect [uploader]="uploader" />
</div> <div class="col-md-9" style="margin-bottom: 40px"> <h3>Upload queue</h3>
<p>Queue length: {{ uploader?.queue?.length }}</p> <table class="table">
<thead>
<tr>
<th width="50%">Name</th>
<th>Size</th>
<th>Progress</th>
<th>Status</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let item of uploader.queue">
<td><strong>{{ item?.file?.name }}</strong></td>
<td *ngIf="uploader.isHTML5" nowrap>{{ item?.file?.size/1024/1024 | number:'.2' }} MB</td>
<td *ngIf="uploader.isHTML5">
<div class="progress" style="margin-bottom: 0;">
<div class="progress-bar" role="progressbar" [ngStyle]="{ 'width': item.progress + '%' }"></div>
</div>
</td>
<td class="text-center">
<span *ngIf="item.isSuccess"><i class="glyphicon glyphicon-ok"></i></span>
<span *ngIf="item.isCancel"><i class="glyphicon glyphicon-ban-circle"></i></span>
<span *ngIf="item.isError"><i class="glyphicon glyphicon-remove"></i></span>
</td>
<td nowrap>
<button type="button" class="btn btn-success btn-xs"
(click)="item.upload()" [disabled]="item.isReady || item.isUploading || item.isSuccess">
<span class="glyphicon glyphicon-upload"></span> Upload
</button>
<button type="button" class="btn btn-warning btn-xs"
(click)="item.cancel()" [disabled]="!item.isUploading">
<span class="glyphicon glyphicon-ban-circle"></span> Cancel
</button>
<button type="button" class="btn btn-danger btn-xs"
(click)="item.remove()">
<span class="glyphicon glyphicon-trash"></span> Remove
</button>
</td>
</tr>
</tbody>
</table> <div>
<div>
Queue progress:
<div class="progress" style="">
<div class="progress-bar" role="progressbar" [ngStyle]="{ 'width': uploader.progress + '%' }"></div>
</div>
</div>
<button type="button" class="btn btn-success btn-s"
(click)="uploader.uploadAll()" [disabled]="!uploader.getNotUploadedItems().length">
<span class="glyphicon glyphicon-upload"></span> Upload all
</button>
<button type="button" class="btn btn-warning btn-s"
(click)="uploader.cancelAll()" [disabled]="!uploader.isUploading">
<span class="glyphicon glyphicon-ban-circle"></span> Cancel all
</button>
<button type="button" class="btn btn-danger btn-s"
(click)="uploader.clearQueue()" [disabled]="!uploader.queue.length">
<span class="glyphicon glyphicon-trash"></span> Remove all
</button>
</div> </div> </div> </div>

markup

     import { Component } from '@angular/core';
import { FileUploader } from 'ng2-file-upload'; // const URL = '/api/';
const URL = 'https://evening-anchorage-3159.herokuapp.com/api/'; @Component({
selector: 'simple-demo',
templateUrl: './simple-demo.html'
})
export class SimpleDemoComponent {
public uploader:FileUploader = new FileUploader({url: URL});
public hasBaseDropZoneOver:boolean = false;
public hasAnotherDropZoneOver:boolean = false; public fileOverBase(e:any):void {
this.hasBaseDropZoneOver = e;
} public fileOverAnother(e:any):void {
this.hasAnotherDropZoneOver = e;
}
}

typescript

    /*eslint-disable*/
var express = require('express');
var multer = require('multer');
var fs = require('fs');
var app = express(); var DIR = './uploads/'; var upload = multer({dest: DIR}); app.use(function (req, res, next) {
res.setHeader('Access-Control-Allow-Origin', 'http://valor-software.github.io');
res.setHeader('Access-Control-Allow-Methods', 'POST');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
}); app.use(multer({
dest: DIR,
rename: function (fieldname, filename) {
return filename + Date.now();
},
onFileUploadStart: function (file) {
console.log(file.originalname + ' is starting ...');
},
onFileUploadComplete: function (file) {
console.log(file.fieldname + ' uploaded to ' + file.path);
}
})); app.get('/api', function (req, res) {
res.end('file catcher example');
}); app.post('/api', function (req, res) {
upload(req, res, function (err) {
if (err) {
return res.end(err.toString());
} res.end('File is uploaded');
});
}); var PORT = process.env.PORT || 3000; app.listen(PORT, function () {
console.log('Working on port ' + PORT);
});

backend demo

API

Usage

import { FileSelectDirective, FileDropDirective, FileUploader } from 'ng2-file-upload/ng2-file-upload';

Annotations

// class FileSelectDirective
@Directive({ selector: '[ng2FileSelect]' })
// class FileDropDirective
@Directive({ selector: '[ng2FileDrop]' })

FileSelect API

Properties

  • uploader - (FileUploader) - uploader object. 上传对象

    Parameters supported by this object(此对象支持的参数:):

  • url - URL of File Uploader's route(URL网址上传文件的路径)

  • authToken - auth token that will be applied as 'Authorization' header during file send.(认证令牌,将作为“授权”标题在文件发送。)
  • disableMultipart - If 'true', disable using a multipart form for file upload and instead stream the file. Some APIs (e.g. Amazon S3) may expect the file to be streamed rather than sent via a form. Defaults to false.(如果'真',禁止使用文件上传多个文件形式而流。一些API(例如Amazon S3)可能希望文件流,而不是通过表单发送。默认为false。)

Angular2 File Upload的更多相关文章

  1. jQuery File Upload 单页面多实例的实现

    jQuery File Upload 的 GitHub 地址:https://github.com/blueimp/jQuery-File-Upload 插件描述:jQuery File Upload ...

  2. jQuery File Upload done函数没有返回

    最近在使用jQuery File Upload 上传图片时发现一个问题,发现done函数没有callback,经过一番折腾,找到问题原因,是由于dataType: ‘json’造成的,改为autoUp ...

  3. kindeditor多图片上传找不到action原来是private File upload成员变量惹得祸

    kindeditor多图片上传找不到action原来是private File upload成员变量惹得祸

  4. 【转发】Html5 File Upload with Progress

    Html5 File Upload with Progress               Posted by Shiv Kumar on 25th September, 2010Senior Sof ...

  5. 用jQuery File Upload做的上传控件demo,支持同页面多个上传按钮

    需求 有这么一个需求,一个form有多个文件要上传,但又不是传统的图片批量上传那种,是类似下图这种需求,一开始是用的swfupload做的上传,但是问题是如果有多个按钮的话,就要写很多重复的代码,于为 ...

  6. jquery file upload 文件上传插件

    1. jquery file upload 下载 jquery file upload Demo 地址:https://blueimp.github.io/jQuery-File-Upload/ jq ...

  7. jQuery File Upload跨域上传

    最近在做一个一手粮互联网项目,方案为前后端分离,自己负责前端框架,采用了Requirejs+avalonjs+jquery三个框架完成. 前后端通过跨域实现接口调用,中间也发现了不少问题,尤其是在富文 ...

  8. 《Play for Java》学习笔记(六)文件上传file upload

    一. Play中标准方法 使用表单form和multipart/form-data的content-type类型. 1.Form @form(action = routes.Application.u ...

  9. [转]Maintain File Upload Control on Postbacks

    本文转自:http://www.ironspeed.com/articles/Maintain%20File%20Upload%20Control/Article.aspx Introduction ...

随机推荐

  1. Dropping TSO features since no CSUM feature

    今天在虚拟机中练习docker的时候突然linux系统反复重启,一下子没有发现原因,不断打开虚拟机还是自动会关机,一下子很纳闷. 打开计算机里,一看原来虚拟机所在磁盘空间满了.怪不得 出现这个问题,清 ...

  2. 中文分词工具thulac4j发布

    1. 介绍 thulac4j是THULAC的Java 8工程化实现,具有分词速度快.准.强的特点:支持 自定义词典 繁体转简体 停用词过滤 若想在项目中使用thulac4j,可添加依赖: <de ...

  3. canvas 水滴图、液体进度条、仿加速球、圆球水波图

    传送门:https://github.com/guoyoujin/WaterMoire <!DOCTYPE html> <html lang="en"> & ...

  4. 导出表结构到Excel 生成代码用

    导出表结构到Excel ,统一维护,生成代码用 SELECT C.TABLE_NAME,CC.COMMENTS,C.COLUMN_NAME,C.DATA_TYPE, CASE WHEN C.DATA_ ...

  5. 【C#】读取Excel中嵌套的Json对象,Json带斜杠的问题(其三)

    除了上一篇中提到的对字符串的字符替换操作,去掉Json中的转义符反斜杠\之外,还发现了更加简单的办法. 就是使用Newtownsoft.Json序列化Json时,将嵌套的Json对象(字符串)转为JO ...

  6. [Linux]Linux read/write

    Read 默认read是block模式,如果想设置非block默认,则open时候参数添加O_NONBLOCK read block模式下,并非等到Buffer满才返回,而是只要有data avaia ...

  7. mac os系统go安装:go install github.com/nsf/gocode: open /usr/local/go/bin/gocode: permission denied

    gocode是go语言代码自动提示工具 安装时进入src目录执行:go get -u github.com/nsf/gocode 出现: github.com/nsf/gocode (download ...

  8. Spark基础

    1 读取本地文件 ./spark-shell scala> val textFile=sc.textFile("file:///home/hadoop/wordfile1.txt&qu ...

  9. 框架源码系列九:依赖注入DI、三种Bean配置方式的注册和实例化过程

    一.依赖注入DI 学习目标1)搞清楚构造参数依赖注入的过程及类2)搞清楚注解方式的属性依赖注入在哪里完成的.学习思路1)思考我们手写时是如何做的2)读 spring 源码对比看它的实现3)Spring ...

  10. 框架源码系列四:手写Spring-配置(为什么要提供配置的方法、选择什么样的配置方式、配置方式的工作过程是怎样的、分步骤一个一个的去分析和设计)

    一.为什么要提供配置的方法 经过前面的手写Spring IOC.手写Spring DI.手写Spring AOP,我们知道要创建一个bean对象,需要用户先定义好bean,然后注册到bean工厂才能创 ...