使用Angular 4上传文件不起作用。结果不明白

时间:2017-08-14 03:30:43

标签: angular angular-file-upload

使用Angular 4上传文件不起作用。结果未定义。

文件信息随事件一起出现,但FormData不起作用并且结果未定义。

Html文件

<form #form="ngForm" enctype="multipart/form-data" novalidate>
    <input type="file" id="sef" name="sef" class="form-control" ngModel  (change)="getSef($event)">      
    <button class="btn btn-primary" (click)="envirArquivos(form.value)">Enviar</button>
</form>

TS文件

import { Component, OnInit, Input } from '@angular/core';

@Component({
    selector: 'mw-compare-nfe',
    templateUrl: './compare-nfe.component.html'
})
export class CompareNFEComponent implements OnInit {

    arquivoZip: any

    getZip(event) {
        let fileList: FileList = event.target.files;
        let file: File = fileList[0];

        console.log(file) . <--- The object file is defined.

        let formData: FormData = new FormData();
        this.arquivoZip = formData.append('File', file, file.name);

    }

    constructor() { }

    ngOnInit() {
    }

    envirArquivos(order: Order) {
      console.log(this.arquivoZip) . <--- Undefined
    }

}

2 个答案:

答案 0 :(得分:0)

FormData.append会将当前值添加/附加到您创建的formData内容中,但 返回该实例,请参阅 {{3} }

因此,您必须附加文件并将其设置为在单独的行中提交的组件

let formData: FormData = new FormData();
formData.append('File', file, file.name);
this.arquivoZip = formData;

答案 1 :(得分:0)

文件上传: - 完整代码

PS C:> $basename = "File.Name.S01.E01"
PS C:> $basename.Replace(".S"," S").Replace(".E","E").Replace(".","")
FileName S01E01

服务

docker exec -it ID gitlab-runner register -n \
--name myrunner \
--url  mygitlab.com\
--registration-token {{TOKEN}} \
--tag-list tag1,tag2 \
--run-untagged true \
--executor shell

More info