Asp.net mvc 如何让MVC返回上下文类型
我在Asp.NET中编写了以下代码,我正在尝试将其转换为MVC,但不确定如何在操作中实现这一点Asp.net mvc 如何让MVC返回上下文类型,asp.net-mvc,pdf-generation,Asp.net Mvc,Pdf Generation,我在Asp.NET中编写了以下代码,我正在尝试将其转换为MVC,但不确定如何在操作中实现这一点 HttpContext context; context.Response.ContentType = "application/pdf"; context.Response.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", filename)); context.Response.WriteFi
HttpContext context;
context.Response.ContentType = "application/pdf";
context.Response.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", filename));
context.Response.WriteFile(filename);
context.Response.Flush();
context.Response.SuppressContent = true;
你的意思是“内容”而不是“上下文”类型,是吗 也许这篇文章会有所帮助:
更新: 因此,您有一个服务器端脚本(
PDF.axd
),用于生成PDF文件。您的文件系统中没有存储pdf文件。在这种情况下,您需要首先获取pdf,然后将其流式传输到客户端:
public ActionResult Download()
{
byte[] pdfBuffer = null;
using (var client = new WebClient())
{
var url = string.Format("PDF.axd?file={0}.pdf", voucherDetail.Guid);
pdfBuffer = client.DownloadData(url);
}
var cd = new ContentDisposition
{
Inline = false,
FileName = "file.pdf"
};
Response.SuppressContent = true;
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(pdfBuffer, MediaTypeNames.Application.Pdf);
}
此控制器操作的有用性值得怀疑,因为您已经有了执行此操作的脚本。这看起来是正确的,但我得到的是“不是有效的虚拟路径”。即使文件和URL正确,您在哪一行得到此异常?
filename
是绝对路径吗?下面是生成文件名的行。字符串文件名=(string.Format(“PDF.axd?file={0}.PDF”,voucherDetail.Guid));我也尝试过使用完整的URL和HTML:\\但是得到了相同的响应在IE的URL中替换文件名可以正常工作,并且可以显示PDF。您混淆了文件名和URL地址。请看我的更新。
public ActionResult Download()
{
byte[] pdfBuffer = null;
using (var client = new WebClient())
{
var url = string.Format("PDF.axd?file={0}.pdf", voucherDetail.Guid);
pdfBuffer = client.DownloadData(url);
}
var cd = new ContentDisposition
{
Inline = false,
FileName = "file.pdf"
};
Response.SuppressContent = true;
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(pdfBuffer, MediaTypeNames.Application.Pdf);
}