I have the following code I wrote in Asp.NET and I am trying to convert it to MVC, but not sure how I do this within an Action
HttpContext context;
context.Response.ContentType = "application/pdf";
context.Response.AppendHeader("Content-Disposition", String.Format("attachment;filename={0}", filename));
context.Response.WriteFile(fil开发者_Python百科ename);
context.Response.Flush();
context.Response.SuppressContent = true;
public ActionResult Download()
{
var cd = new ContentDisposition
{
Inline = false,
FileName = filename
};
Response.SuppressContent = true;
Response.AppendHeader("Content-Disposition", cd.ToString());
return this.File(filename, MediaTypeNames.Application.Pdf);
}
UPDATE:
So you have a server side script (PDF.axd
) which generates the PDF file. You don't have the pdf file stored on your file system. In this case you will need to first fetch the pdf and then stream it to the client:
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);
}
The usefulness of this controller action is doubtful as you already have a script that does the job.
You meant 'Content' and not 'Context' type, yes?
Maybe this SO post will help: ASP.NET MVC and text/xml content type
精彩评论