Andrus Andrus - 1 month ago 12
ASP.NET (C#) Question

how to specify name for saving pdf from browser

ASP.NET MVC 4 controller returs pdf:

public ActionResult Pdf() {

byte[] result = CreatePDF();
var ms = new MemoryStream(result);
return new FileStreamResult(ms, "application/pdf");
}


Results appears in Chrome browser window.
If user right-clicks in pdf and selects Save as Chrome offers
Pdf.pdf
as file name to save.

How to specify other default file name like
Order 123.pdf
?

Chrome Developer Tool show that file nime is not speficied in response:

Cache-Control:private, s-maxage=0
Content-Length:30814
Content-Type:application/pdf
Date:Fri, 14 Oct 2016 19:18:09 GMT
Server:Microsoft-IIS/7.5
X-AspNet-Version:4.0.30319

Answer

Try using File method.

public FileResult Pdf()  
{
    byte[] result = CreatePDF();
    string fileName = "Order 123.pdf";
    return File(result, "application/pdf", fileName);
}

There is no need to wrap result in MemoryStream