views:

109

answers:

2

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(filename);
context.Response.Flush();
context.Response.SuppressContent = true;
A: 

You meant 'Content' and not 'Context' type, yes?

Maybe this SO post will help: ASP.NET MVC and text/xml content type

o.k.w
HttpContext context;
Coppermill
Ah ok, I misunderstood. :P
o.k.w
+3  A: 
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.

Darin Dimitrov
This looks correct, but I'm getting "is not a valid virtual path." even though the file and the URL are correct
Coppermill
On which line do you get this exception? Is `filename` an absolute path?
Darin Dimitrov
Here is the line that is generating the filename. string filename = (string.Format("PDF.axd?file={0}.pdf", voucherDetail.Guid));I have also tried it using the full URL with HTML:\\ but get the same response
Coppermill
placing the filename in the URL of IE works fine and a PDF gets displayed
Coppermill
You are confusing `a filename` and an `url address`. Please see my update.
Darin Dimitrov
Got it, it was the full path on the server I had to give, all working now :-)
Coppermill
string filename = Server.MapPath(string.Format("..\\..\\app_data\\GiftVouchers\\{0}.pdf", voucherDetail.Guid));
Coppermill
Thanks for the update, this is working like a dream on my development machine, but for some reason on the live box, IIS 6 it take about 20 seconds to bring up the save as box on all browsers, it seems to be taking its time on return File(pdfBuffer, MediaTypeNames.Application.Pdf); any ideas?
Coppermill
How did you determine that it is exactly this line that takes 20 seconds?
Darin Dimitrov
I watch the browser, anyhow I found the answer http://stackoverflow.com/questions/1928494/mvc-action-taking-a-long-time-to-return/1928702#1928702
Coppermill