Andrus Andrus - 1 year ago 62
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
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
Date:Fri, 14 Oct 2016 19:18:09 GMT

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download