How to angular2 post JSON data and files In same request

后端 未结 4 2232
谎友^
谎友^ 2020-12-06 01:16

I want to implement post file and Json data in the same request .

below is the upload file code :

upload(url:string,file:File):Observable<{compl         


        
4条回答
  •  不知归路
    2020-12-06 01:40

    The way my manager @Jesse come up with is like:

    public uploadFiles(id: ServerID, brd: File, sch: File, args: any[]): Observable {
            const data = new FormData();
            data.append('brd', brd);
            data.append('sch', sch);
            data.append('data', JSON.stringify(args));
            return this.httpClient.post(URL, data, {
                responseType: 'blob',
            });
        }
    

    The definition of the FormData append() is append(name: string, value: string | Blob, fileName?: string): void; which allows you to append JSON parameters to it or upload a file.

提交回复
热议问题