我正在探索下面的Angular 2指令来进行文件上传。在尝试
时ng-file-select指令
import {
Directive,
ElementRef,
EventEmitter,
Input,
Output,
HostListener
} from '@angular/core';
import { Ng2Uploader } from '../services/ng2-uploader';
@Directive({
selector: '[ngFileSelect]'
})
export class NgFileSelectDirective {
@Input() events: EventEmitter<any>;
@Output() onUpload: EventEmitter<any> = new EventEmitter();
@Output() onPreviewData: EventEmitter<any> = new EventEmitter();
_options:any;
get options(): any {
return this._options;
}
@Input('options')
set options(value: any) {
this._options = value;
this.uploader.setOptions(this.options);
}
files: any[] = [];
uploader: Ng2Uploader;
constructor(public el: ElementRef) {
this.uploader = new Ng2Uploader();
setTimeout(() => {
this.uploader.setOptions(this.options);
});
this.uploader._emitter.subscribe((data: any) => {
this.onUpload.emit(data);
if (data.done) {
this.files = this.files.filter(f => f.name !== data.originalName);
}
});
this.uploader._previewEmitter.subscribe((data: any) => {
this.onPreviewData.emit(data);
});
setTimeout(() => {
if (this.events) {
this.events.subscribe((data: string) => {
if (data === 'startUpload') {
this.uploader.uploadFilesInQueue();
}
});
}
});
}
filterFilesByExtension(): void {
this.files = this.files.filter(f => {
if (this.options.allowedExtensions.indexOf(f.type) !== -1) {
return true;
}
let ext: string = f.name.split('.').pop();
if (this.options.allowedExtensions.indexOf(ext) !== -1 ) {
return true;
}
return false;
});
}
@HostListener('change') onChange(): void {
this.files = Array.from(this.el.nativeElement.files);
if (this.options.filterExtensions && this.options.allowedExtensions) {
this.filterFilesByExtension();
}
if (this.files.length) {
this.uploader.addFilesToQueue(this.files);
}
}
}
ng-file-select service
import {
Directive,
ElementRef,
EventEmitter,
Input,
Output,
HostListener
} from '@angular/core';
import { Ng2Uploader } from '../services/ng2-uploader';
@Directive({
selector: '[ngFileSelect]'
})
export class NgFileSelectDirective {
@Input() events: EventEmitter<any>;
@Output() onUpload: EventEmitter<any> = new EventEmitter();
@Output() onPreviewData: EventEmitter<any> = new EventEmitter();
_options:any;
get options(): any {
return this._options;
}
@Input('options')
set options(value: any) {
this._options = value;
this.uploader.setOptions(this.options);
}
files: any[] = [];
uploader: Ng2Uploader;
constructor(public el: ElementRef) {
this.uploader = new Ng2Uploader();
setTimeout(() => {
this.uploader.setOptions(this.options);
});
this.uploader._emitter.subscribe((data: any) => {
this.onUpload.emit(data);
if (data.done) {
this.files = this.files.filter(f => f.name !== data.originalName);
}
});
this.uploader._previewEmitter.subscribe((data: any) => {
this.onPreviewData.emit(data);
});
setTimeout(() => {
if (this.events) {
this.events.subscribe((data: string) => {
if (data === 'startUpload') {
this.uploader.uploadFilesInQueue();
}
});
}
});
}
filterFilesByExtension(): void {
this.files = this.files.filter(f => {
if (this.options.allowedExtensions.indexOf(f.type) !== -1) {
return true;
}
let ext: string = f.name.split('.').pop();
if (this.options.allowedExtensions.indexOf(ext) !== -1 ) {
return true;
}
return false;
});
}
@HostListener('change') onChange(): void {
this.files = Array.from(this.el.nativeElement.files);
if (this.options.filterExtensions && this.options.allowedExtensions) {
this.filterFilesByExtension();
}
if (this.files.length) {
this.uploader.addFilesToQueue(this.files);
}
}
}
提供选项时:
this.options = {
url: 'http://api.ng2-uploader.com:10050/upload',
filterExtensions: true,
allowedExtensions: ['image/png', 'image/jpg'],
calculateSpeed: true,
multiple:true,
maxUploads:1,
data: {
userId: 12,
isAdmin: true
},
customHeaders: {
'custom-header': 'value'
},
authToken: 'asd123b123zxc08234cxcv',
authTokenPrefix: 'Bearer'
};
来源:my answer to your previous question
我想允许多个文件上传,但是应该逐个上传1个文件。
目前在上述指令中似乎没有对此进行任何处理。任何人都可以指导在服务中应该限制的地点和方式。
答案 0 :(得分:0)
我认为您只需要使用简单的html multiple属性,例如
<input type="file" ngFileSelect [options]="options" multiple>
答案 1 :(得分:0)
我建议您使用ng2-file-upload指令。它执行多文件上载,方法是将它们添加到队列中,然后在单独的请求中上传每个文件。