Partager via


Controller.File Method (String, String, String)

Creates a FilePathResult object by using the file name, the content type, and the file download name.

Namespace:  System.Web.Mvc
Assembly:  System.Web.Mvc (in System.Web.Mvc.dll)

Syntax

'Declaration
Protected Friend Overridable Function File ( _
    fileName As String, _
    contentType As String, _
    fileDownloadName As String _
) As FilePathResult
protected internal virtual FilePathResult File(
    string fileName,
    string contentType,
    string fileDownloadName
)
protected public:
virtual FilePathResult^ File(
    String^ fileName, 
    String^ contentType, 
    String^ fileDownloadName
)

Parameters

  • fileName
    Type: System.String
    The path of the file to send to the response.
  • fileDownloadName
    Type: System.String
    The file name to use in the file-download dialog box that is displayed in the browser.

Return Value

Type: System.Web.Mvc.FilePathResult
The file-stream result object.

Remarks

The fileDownloadName parameter is used to generate the content-disposition header. The result object that is prepared by this method is written to the response by the MVC framework when the object is executed. The MediaTypeNames class can be used to get the MIME type for a specific file name extension.

Examples

A Visual Studio project with source code is available to accompany this topic: Download.

The following example shows how to open a file-download dialog box and set the file name to the name of the server file. The id parameter contains the server file name.

public ActionResult ShowFileFNdlNm(string id) {
    string mp = Server.MapPath("~/Content/" + id);
    return File(mp, "text/plain", id);
}
Public Function ShowFileFNdlNm(ByVal id As String) As ActionResult 
    Dim mp As String = Server.MapPath("~/Content/" & id) 
    Return File(mp, "text/plain", id) 
End Function

See Also

Reference

Controller Class

File Overload

System.Web.Mvc Namespace