Returning a file using asp.net mvc?
I have a custom IHttpHandler which is u开发者_如何转开发sed by thick clients to download files using a url such as
http://url.ashx?id=123&version=456
the code handler basically ends with
context.Response.WriteFile(myLocalServerPath);
Is it possible to replace this using the typical asp.net mvc controller pattern ?
In an action:
byte[] fileBytes = ...;
string fileName = "example";
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
(Or a more specific MIME type if known)
From this site but simplified:
public FileResult Download()
{
string filename = "test.pdf";
string contentType = "application/pdf";
//Parameters to file are
//1. The File Path on the File Server
//2. The content type MIME type
//3. The parameter for the file save by the browser
return File(filename, contentType,"Report.pdf");
}
精彩评论