首页 > 解决方案 > Angular 8:服务器正在获取垃圾而不是文件数据

问题描述

我正在使用以下代码将文件上传到服务器。

import {HttpClient} from '@angular/common/http';

fileChange(event) {
let fileList: FileList = event.target.files;
let file: File = fileList[0];
let body: FormData = new FormData();
body.append('uploadFile', file, file.name);
this.http.put('http://localhost:8000', body).subscribe (data => {console.log ('response',data);});
}

我正在使用我开发的 HTTP 服务器,因此我可以看到来自客户端的所有数据。出于某种原因,在 PUT 之后,服务器正在获取垃圾而不是文件中的数据。chrome终端上没有错误消息。

标签: angularuploadhttpclient

解决方案


根据铬,没有身体。以下是请求标头:

选项/HTTP/1.1 主机:localhost:8000 连接:keep-alive 访问控制请求方法:PUT 来源:http://localhost:4200 用户代理:Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit /537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36 Accept: / Sec-Fetch-Site: same-site Sec-Fetch-Mode: cors Referer: http://localhost:4200/timing Accept-Encoding: gzip, deflate, br 接受语言:en-US,en;q=0.9

谢谢你,兹维卡


推荐阅读