【问题标题】:How to send multipart file from angular 4 using http如何使用http从角度4发送多部分文件
【发布时间】:2018-05-01 16:32:35
【问题描述】:

我需要使用我的 spring 控制器期望的 multipar 文件发送一个文件。

public void test(@RequestParam("file") MultipartFile file) {}

我知道我必须使用FormData,但我没能成功。这是我的代码:

this.headers = new Headers({ 'Content-Type': 'multipart/form-data'});
this.options = new RequestOptions({ headers: this.headers });

return this.http.post(environment.SERVER_ENDPOINT + 'parameters/area', formData, options)
  .toPromise()
  .then(this.extractData)
  .catch(this.handleError);

【问题讨论】:

  • 有什么问题?错误? formData 是否发送了类型文件?

标签: spring angular spring-mvc multipartform-data


【解决方案1】:

使用 XMLHttpRequest

let fd = new FormData();
fd.append("file", file);
let xhr = new XMLHttpRequest();
const url = environment.SERVER_ENDPOINT + 'parameters/area';
xhr.open('POST', url, true);
xhr.send(fd);

【讨论】:

    猜你喜欢
    • 2023-04-08
    • 2016-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-11
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    相关资源
    最近更新 更多