C# angular 4下载pdf文件为空
您好,我正在尝试从服务器上创建的报告下载pdf。当我创建它时,我会在服务器上保存一会儿,然后再打第二个电话下载它 以下是web API上的代码:C# angular 4下载pdf文件为空,c#,angular,C#,Angular,您好,我正在尝试从服务器上创建的报告下载pdf。当我创建它时,我会在服务器上保存一会儿,然后再打第二个电话下载它 以下是web API上的代码: var rootDirectory = HostingEnvironment.MapPath("~"); var filePath = $@"{rootDirectory}\Temp\{file}.pdf"; var bytes = File.ReadAllBytes(filePath);
var rootDirectory = HostingEnvironment.MapPath("~");
var filePath = $@"{rootDirectory}\Temp\{file}.pdf";
var bytes = File.ReadAllBytes(filePath);
File.Delete(filePath);
var memoryStream = new MemoryStream(bytes);
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(memoryStream.ToArray())
};
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "report.pdf"
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return result;
然后我回到typescript,下面是我下载时使用的代码:
this.reportsService
.download(id)
.subscribe(r => {
let pdf =r._body;
var pdfName = `${this.selectedReport.name}.pdf`;
let blob = new Blob([pdf], { type: 'application/pdf' });
window.open(URL.createObjectURL(blob));
});
});
我应该提到,当我从服务器上保存pdf的位置打开pdf时,它是正确的。
有人能告诉我我遗漏了什么吗?调试时,
r.\u body
的值是多少?看起来像是下载的字节。有人知道吗?