Kumar
Kumar

Reputation: 11329

Returning a file using asp.net mvc?

I have a custom IHttpHandler which is used 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 ?

Upvotes: 1

Views: 2415

Answers (2)

Seth
Seth

Reputation: 8373

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");
    }

Upvotes: 0

Ian Henry
Ian Henry

Reputation: 22403

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)

Upvotes: 3

Related Questions