Angular File Upload

后端 未结 12 2142
名媛妹妹
名媛妹妹 2020-11-22 10:04

I\'m a beginner with Angular, I want to know how to create Angular 5 File upload part, I\'m trying to find any tutorial or doc, but I don\'t see anything a

12条回答
  •  无人共我
    2020-11-22 11:00

    I am using Angular 5.2.11, I like the solution provided by Gregor Doroschenko, however I noticed that the uploaded file is of zero bytes, I had to make a small change to get it to work for me.

    postFile(fileToUpload: File): Observable {
      const endpoint = 'your-destination-url';
      return this.httpClient
        .post(endpoint, fileToUpload, { headers: yourHeadersConfig })
        .map(() => { return true; })
        .catch((e) => this.handleError(e));
    }
    

    The following lines (formData) didn't work for me.

    const formData: FormData = new FormData();
    formData.append('fileKey', fileToUpload, fileToUpload.name);
    

    https://github.com/amitrke/ngrke/blob/master/src/app/services/fileupload.service.ts

提交回复
热议问题