Send multipart/form-data files with angular using $http

后端 未结 3 1076
予麋鹿
予麋鹿 2020-11-30 01:17

I know there are a lot of questions about this, but I can\'t get this to work:

I want to upload a file from input to a server in multipart/form-data

I\'ve tr

3条回答
  •  囚心锁ツ
    2020-11-30 01:42

    In Angular 6, you can do this:

    In your service file:

     function_name(data) {
        const url = `the_URL`;
        let input = new FormData();
        input.append('url', data);   // "url" as the key and "data" as value
        return this.http.post(url, input).pipe(map((resp: any) => resp));
      }
    

    In component.ts file: in any function say xyz,

    xyz(){
    this.Your_service_alias.function_name(data).subscribe(d => {   // "data" can be your file or image in base64 or other encoding
          console.log(d);
        });
    }
    

提交回复
热议问题