Reputation: 33
We have a module to generate and store report in PDF format.
The url to get document for example like this:
https://domain.com/Document/GetDocument?documentId=00000000-0000-0000-0000-000000000000
This url will return a document with name: Document_UserName_Date
.
However, since there are browser can view PDF file like Chrome, the document will be view right in a tab of browser with above url.
So, when users try to save that document in their computers, the default file name(which get from url) is: https___domain.com.pdf
instead of Document_UserName_Date.pdf
as we expected.
So I'm thinking, if I can just changed the url into:
https://domain.com/Document/Document_UserName_Date.pdf
my problem will be solved.
Upvotes: 0
Views: 468
Reputation: 1038720
In order to avoid the document being shown in the browser, but the user directly prompted to download it, you could use set the Content-Disposition
header to attachment
:
Content-Disposition: attachment; filename="Document_UserName_Date.pdf"
This could be done by simply passing the filename as third argument to the File overload:
public ActionResult GetDocument(Guid documentId)
{
byte[] document = GetDocument(documentId);
return File(document, "application/pdf", "Document_UserName_Date.pdf");
}
UPDATE:
If you want the user to view the document inline in his browser then you could use routing and define the following route:
routes.MapRoute(
"ViewPdfRoute",
"document/{id}/{name}.pdf",
new { controller = "Home", action = "GetDocument" }
);
and in your controller action:
public ActionResult GetDocument(Guid id, string name)
{
byte[] pdf = GetDocument(id);
return File(pdf, "application/pdf");
}
And finally you could request a document like this:
http://domain.com/document/0DF7E254-0576-4BC0-8B05-34FC0F5246A2/document_username_date.pdf
Upvotes: 2