在Angular中,我正在尝试从我的Web API服务器下载Excel文件。
如果我像这样从组件调用我的API:
文件下载很顺利,没有处理响应,我得到了期望的行为: 我该如何在我的 Angular DownloadService 中发出请求,并在
请注意,服务器是这样创建响应的:
如果我像这样从组件调用我的API:
<a href="http://localhost:55820/api/download">Download</a>
文件下载很顺利,没有处理响应,我得到了期望的行为: 我该如何在我的 Angular DownloadService 中发出请求,并在
.subscribe()
中处理结果以获得相同的结果?this.downloadService.download()
.subscribe(file => {/*What to put here for file downloading as above*/});
请注意,服务器是这样创建响应的:
byte[] b = File.ReadAllBytes(HttpRuntime.AppDomainAppPath + "/files/" + "file.xlsx");
var dataStream = new MemoryStream(b);
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new StreamContent(dataStream);
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "File";
response.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping("file.xlsx"));
return response;
提前感谢!:)