2017-08-17 151 views
2

我有一個Ionic 2 PWA,所以它應該在瀏覽器中運行。我希望用戶可以將文件上傳到服務器。由於離子本機文件選擇器僅適用於Android,因此我無法在瀏覽器中使用它。所以我的想法是使用type =「file」的輸入字段。但我的問題是,我只獲取文件名而不是路徑。要上傳文件,我需要路徑。起初,我已經嘗試過用ngModel,然後用離子型表單生成器。這是我的代碼與表單生成器:Ionic 2從瀏覽器上傳文件

TS:

import {Component} from '@angular/core'; 
import {Validators, FormBuilder, FormGroup } from '@angular/forms'; 

@Component({ 
    selector: 'page-test', 
    templateUrl: 'test.html', 
}) 
export class TestPage { 

    private file : FormGroup; 

    constructor(private formBuilder: FormBuilder) { 
     this.file = this.formBuilder.group({ 
      image: [''] 
     }); 
    } 

    logForm(){ 
     console.log(this.file.value) 
    } 

} 

HTML:

... 
<ion-content padding> 
    <form [formGroup]="file" (ngSubmit)="logForm()"> 
     <input type="file" size="50" formControlName="image"> 
     <button ion-button type="submit">Submit</button> 
    </form> 
</ion-content> 

但就像我說的,控制檯只記錄的文件名:

對象{image:「2970.jpg」}

如果我登錄「this.file」(沒有.value),我發現沒有文件對象或類似的東西。有沒有辦法讓離子瀏覽器應用程序中的文件路徑上傳到服務器?

回答

1

我不知道你是否已經找到了解決辦法,但我無論如何都會發布...

對於這個解決方案不需要任何外部NPM模塊。這裏是一步一步的

運行

$ ionic generate component file-uploader 

然後創建新元件複製和粘貼如下

的src /組件/文件的上傳/文件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 /組件/文件上傳/文件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 /組件/文件的上傳/文件uploader.scss

file-uploader { 
    [type=file] { 
     display: none; 
    } 
} 

的src /頁/家庭/ home.html的

<file-uploader [apiUrl]="apiUrl" [params]="[{'key':'language_code', 'value': languageCode}]"></file-uploader> 

所有你需要的現在要做的就是將組件加載到NgModule中並使用它。

我應該提到的最後一件事是,如果您遇到未定義屬性的問題,例如apiUrl,你應該在ngOnInit()方法中初始化它們。

希望可以幫到