我在后端使用.net Web API。有一种从服务器下载文件的方法。它是:
[HttpGet]
[Route("recordFile")]
public BaseResponse RecordFile()
{
ViewModel data;
// ...something with data
string recordPath = data.RecordPath;
string fullpath = HttpContext.Current.Server.MapPath(recordPath);
string contentType = "application/" + Path.GetExtension(fullpath).Substring(1);
WebClient req = new WebClient();
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
response.ContentType = contentType;
response.AddHeader("Content-Disposition", "attachment;filename=\"" + fullpath + "\"");
response.BinaryWrite(req.DownloadData(fullpath));
response.End();
return OkRes(null, "Success", StatusCodeConstant.Ok);
}
当我从邮递员点击上述方法时,邮递员会自动打开对话框以保存文件。
但是当使用angular下载文件时,它会返回null
数据,并且不会下载文件。
我正在用以下代码编写代码:
downloadRecordFile(): void {
this._HttpClient.get(`${this.apiRoot}/api/recordFile`).toPromise().then(data => {
console.log(data); // returns null
});
}
任何帮助将不胜感激。