代码之家  ›  专栏  ›  技术社区  ›  Raj

文件上传适用于postman,但不适用于angular2[重复]

  •  1
  • Raj  · 技术社区  · 7 年前

    我对Angular 2很陌生,请考虑一下。

    我在使用angular 2将文件上载到节点服务器时遇到问题。使用邮递员可以很好地工作:

    邮差快照:

    enter image description here

    但是,当我使用Angular2应用程序向服务器发送文件时,它不会执行任何操作:

    角度HTML组件

    <label for="file1">Upload PDF version: </label>
    <form class="form-inline">
        <div class="form-group">
            <input type="file" id="file1" #abc name="fileToUpload"  class="form-control" placeholder="Upload PDF">
        </div>
        <button class="btn btn-primary" type="submit" (click)="onUpload(abc)">Upload</button>
    </form>
    

    角度TS组件:

    import { Component, OnInit } from '@angular/core';
    import { UploadService } from './upload.service';
    import { NgForm } from '@angular/forms';
    
    @Component({
      selector: 'app-upload',
      templateUrl: './upload.component.html',
      styleUrls: ['./upload.component.css']
    })
    export class UploadComponent implements OnInit {
    
      // file1 = document.getElementById("file1");
      constructor(private uploadService: UploadService) { }
    
      ngOnInit() {
      }
    
      onUpload = (file: File) => {
        this.uploadService.uploadfile(file)
          .subscribe((res) => {
            console.log(res);
          },
    
            (err) => {
              console.log(err);
            });
      }
    
    }
    

    服务组件:

    import { Injectable } from '@angular/core';
    import { Observable } from 'rxjs/Observable';
    import { HttpClient } from '@angular/common/http';
    
    
    @Injectable()
    export class UploadService {
    
      constructor(private http: HttpClient) {
      }
    
      uploadfile(file: File): Observable<File> {
    
        console.log(file);
        return this.http.post<File>("http://localhost:3000/fileupload", file, { headers: { 'Content-Type': 'application/json' } });
      }
    
    }
    

    当然,我一定错过了什么。请大家提供一些解决方案。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Lakshmi Prasanna    7 年前

    您必须创建formData才能上载文件。下面是示例代码。

          fileDetails: any = {
            clientDoc: {}
          }; 
          uploadClientDoc(event) {
            this.fileDetails.clientMandateForm = event.srcElement.files[0];
            this.submitFlag = this.fileDetails.isdaFile ? true : false;
          }
    
          submit() {
            let formData: FormData = new FormData();
            formData.append('clientDoc', this.fileDetails.clientDoc);
            var data = this.urService.uploadFiles(formData).subscribe(res => {
              //your login  
            });
    
          }
    
    
    
          uploadFiles(data) {
            const httpOptions = {
              headers: new HttpHeaders({        
                'Accept': 'application/json',
                'Access-Control-Allow-Origin': '*',
                'Content-Type': 'multipart/form-data' 
              })
            };
    
            return this.http.post('<url>', data, httpOptions);
          }
    
    
    
    
    
    
           <div>
            <label>Please Select Document</label>
            <label class="type-file">+ Upload FIle
              <input type="file" accept="application/pdf" id="my_file1" (change)="uploadClientDoc($event)" />
            </label>   
            <label >{{fileDetails.clientDoc.name}}</label>
          </div>
    
          <button class="ci-btn float-right-btn" type="button"  (click)="submit()" value="Submit">Submit</button>
    
        2
  •  0
  •   Yanis-git    7 年前

    我认为你的问题是关于标题。

    尝试以下操作:

    import { Injectable } from '@angular/core';
    import { Observable } from 'rxjs/Observable';
    import { HttpClient } from '@angular/common/http';
    
    
    @Injectable()
    export class UploadService {
    
      constructor(private http: HttpClient) {
      }
    
      uploadfile(file: File): Observable<File> {
    
        console.log(file);
        return this.http.post<File>("http://localhost:3000/fileupload", file, { headers: { 'Content-Type': 'application/x-www-form-urlencoded' } });
      }
    
    }
    

    如果你检查你的邮差截图,是完全相同的标题提供。是上载文件的常见方式