我下载了控制器方法生成的PDF文件(不打开(。文件正在新的单独窗口中打开。我正在服务器端生成MemoryStream。我必须将它返回到同一个窗口中的客户端,在这里我不必打开,只需在同一个客户端窗口中下载即可。下面的代码我试过了-
服务器-
public async Task<ActionResult> DownloadReport(string id, string reportType="")
{
var fileData = await GetReport(id, reportType);
// here fileData is MemoryStream
return File(fileData, "application/pdf");
}
html代码-
@Html.ActionLink("Download", "DownloadReport","Files", new { id = "abc" },null)
使用Content-Disposition
标头
public async Task<ActionResult> DownloadReport(string id, string reportType="")
{
var fileData = await GetReport(id, reportType);
// here fileData is MemoryStream
Response.AddHeader("Content-Disposition", "attachment;filename=file.pdf");
return File(fileData, "application/pdf");
}