我正在将应用程序写入Angular并且必须上传文件。网络显示状态:200但没有响应,我的后端没有收到任何文件。
问题是.. 1.为什么请求方法是选项?(CORS可能与此有关!但我已经在另一台机器上进行了测试,其中CORS不是问题,因为它在同一端口上)
2.如何在使用文件上传代码时将Content-Type从text / plain更改为multipart / form-data?
这是使用文件上传器时浏览器的网络报告:
Request URL:http://localhost:5000/clari5-cc/rest/1.0/installation/upload
Request Method:OPTIONS
Status Code:200 OK
Remote Address:[::1]:5000
Response Headers
view source
Allow:POST, OPTIONS
Content-Type:text/plain
Request Headers
view source
Accept:*/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:
Access-Control-Request-Method:POST
Connection:keep-alive
Host:localhost:5000
Origin:http://evil.com/
Referer:http://localhost:3000/
但我的旧版运行应用程序曾经在网络响应中获取此数据:我可以看到一个显着的差异。
Request URL:http://localhost:5000/try/rest/1.0/installation/upload
Request Method:POST
Status Code:200 OK
Remote Address:[::1]:5000
Response Headers
view source
Content-Type:application/json
Transfer-Encoding:chunked
Request Headers
view source
Accept:application/json, text/javascript, */*; q=0.01
Accept-Encoding:gzip, deflate
Content-Type:multipart/form-data; boundary=---- WebKitFormBoundaryB06sfkK4FgTODz0F
Cookie:Webstorm-e27c5fb2=42da3160-45c3-490c-a5d9-b0ed07b63022
Host:localhost:5000
Origin:http://evil.com/
Referer:http://localhost:5000/try/mainPage.html
Request Payload
------WebKitFormBoundaryB06sfkK4FgTODz0F
Content-Disposition: form-data; name="uploadedFiles"; filename="installer.tar"
Content-Type: application/x-tar
这里是代码,我没有任何线索如何进行更改,没有提供选项也没有例子? 任何帮助和建议将不胜感激,谢谢
export class AppComponent {
public uploader:FileUploader = new FileUploader({url: "http://localhost:5000/try/rest/1.0/installation/upload"});
public hasBaseDropZoneOver:boolean = false;
public hasAnotherDropZoneOver:boolean = false;
public fileOverBase(e:any):void {
this.hasBaseDropZoneOver = e;
}
public fileOverAnother(e:any):void {
this.hasAnotherDropZoneOver = e;
}
}
或
export class AppDemoComponent implements OnInit {
private zone: NgZone;
private basicOptions: Object;
private progress: number = 0;
private response: any = {};
ngOnInit() {
this.zone = new NgZone({ enableLongStackTrace: false });
this.basicOptions = {
url: 'http://api.ng2-uploader.com:10050/upload'
};
}
handleUpload(data: any): void {
this.zone.run(() => {
this.response = data;
this.progress = data.progress.percent / 100;
});
}
}