我正在Angular 2上做一个项目,我正在开始实施一个下载方法,但在测试时我注意到我的服务器没有获取文件的路径。我可能犯了一个我没有看到的错误。 这是我的代码:Angular2 - 没有收到服务器上的帖子参数
.component.ts
downloadFile(filePath: string): void{
this.dataImportService.downloadFile(filePath)
.then(data => {
console.log(data);
});
}
dataImportService.ts
downloadFile(filePath: string): any{
return this.http.post(SERVICE_URL + 'DataImport/downloadFile', filePath)
.toPromise()
.then(response => {
return response.json().ReturnStatus.ReturnObject;
})
.catch(this.handleError);
}
分析铬控制台上的帖子,正在发送的文件路径,但是,当它得到我的服务器(C#API)它为空。
DataImportController.cs
[HttpPost]
public void downloadFile(string path)
{
try
{
string extension = Path.GetExtension(path).Substring(1);
string fileName = Path.GetFileName(path);
HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
response.ClearHeaders();
response.AddHeader("Content-Type", Utils.MimeTypesConverter.GetMimeType(extension));
response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
response.TransmitFile(Server.MapPath(path));
response.End();
}
catch (Exception ex)
{
Response.Write(ex.Message);
return;
}
}
有什么建议? 非常感谢您的帮助。
通过查询字符串,'this.http.post(SERVICE_URL + 'DataImport/downloadFile?文件路径=' + filePath' – anoop
通并改变方法得到?因为http.post需要更多的参数 –