我不知道你是否已经找到了解决方案,但无论如何我都会发布它......
对于这个解决方案,您不需要任何外部 npm 模块。这是一步一步的
通过运行创建新组件
$ ionic generate component file-uploader
然后复制粘贴下面的代码
src/components/file-uploader/file-uploader.ts
import { Component, ViewChild, ElementRef, Input } from '@angular/core';
import 'rxjs/add/operator/map';
import { Observable } from 'rxjs/Observable';
import { HttpClient } from '@angular/common/http';
/**
* Usage
*
* <file-uploader [apiUrl]="apiUrl" [params]="uploadParams"></file-uploader>
*
* <file-uploader [apiUrl]="apiUrl" [params]="[{'key':'language_code', 'value': 'en'}]"></file-uploader>
*
*/
@Component({
selector: 'file-uploader',
templateUrl: 'file-uploader.html'
})
export class FileUploaderComponent
{
@ViewChild('file') fileInput: ElementRef;
@Input('apiUrl') apiUrl: string = null;
@Input('params') params: Array<{key: string, value: string}> = [];
@Input('buttonText') buttonText: string = 'Upload';
@Input('buttonType') buttonType: 'button' | 'icon' = 'icon';
@Input('icon') icon: string = 'cloud-upload';
@Input('onUploadSuccess') onUploadSuccess: (file: File, response: any) => void
= function (file: File, response: any) { console.log(file); console.log(response); };
@Input('onUploadError') onUploadError: (file: File) => void = function (error: any) { console.log(error) };
fileToUpload: File = null;
constructor(private httpClient: HttpClient)
{
}
triggerFileInputClick()
{
this.fileInput.nativeElement.click();
}
onFileInputChange(files: FileList)
{
this.fileToUpload = files.item(0);
if (this.fileInput.nativeElement.value != '')
{
this.upload();
}
}
upload()
{
const formData: FormData = new FormData();
formData.append('file', this.fileToUpload, this.fileToUpload.name);
this.params.map(param => {
formData.append(param.key, param.value);
});
let headers = {};
this.httpClient
.post(this.apiUrl, formData, { headers: headers })
// .map(() => { return true; })
.subscribe(response => {
this.onUploadSuccess(this.fileToUpload, response);
this.fileInput.nativeElement.value = '';
}, error => {
this.onUploadError(error);
});
}
}
src/components/file-uploader/file-uploader.html
<div>
<input type="file" #file (change)="onFileInputChange($event.target.files)">
<button *ngIf="buttonType == 'button'" ion-button (click)="triggerFileInputClick()">{{buttonText}}</button>
<ion-icon *ngIf="buttonType == 'icon'" name="cloud-upload" (click)="triggerFileInputClick()"></ion-icon>
</div>
src/components/file-uploader/file-uploader.scss
file-uploader {
[type=file] {
display: none;
}
}
src/pages/home/home.html
<file-uploader [apiUrl]="apiUrl" [params]="[{'key':'language_code', 'value': languageCode}]"></file-uploader>
您现在需要做的就是在 NgModule 中加载组件并使用它。
我要提到的最后一件事是,如果您遇到未定义属性的问题,例如apiUrl,你应该在 ngOnInit() 方法中初始化它们。
希望有帮助
更新
不要忘记将文件上传器导入到使用上传器的组件中,否则你会得到 Can't bind to 'apiUrl' because it is not a known property of 'file-uploader'错误。
我所做的是在 components 文件夹中创建了新的模块 components.module.ts 文件。然后我将文件上传器组件导入其中。为了使用它,我将 components 模块导入到使用文件上传器组件的组件中。
src/components/components.module.ts
import { IonicModule } from 'ionic-angular';
import { NgModule } from '@angular/core';
import { FileUploaderComponent } from './file-uploader/file-uploader';
@NgModule({
declarations: [
FileUploaderComponent
],
imports: [
IonicModule,
],
exports: [
FileUploaderComponent
]
})
export class ComponentsModule {}
src/pages/home/home.module.ts
import { ComponentsModule } from '../../components/components.module';