Saturday 15 August 2020

Unable to download video file in ASP.NET MVC

 I am using following code to download video file in ASP.net MVC but still unable to download.

public FileResult GetFile()
{
    string filename = string.Empty;
    byte[] file = null;
    string path = string.Empty;
    filename = "testVideo.mp4";
    path = Server.MapPath("location path here");
    System.IO.FileInfo fileinfo = new System.IO.FileInfo(path);
    file = System.IO.File.ReadAllBytes(path);
    return File(file, "content-disposition");
}
Answer:

You need to specify a MIME type as the second parameter of the File function:

return File(file, "video/mp4");

If you want to force the file to download, rather than be viewed, then you can also set the filename on it:

return File(file, "video/mp4", "myfilename.mp4");

No comments:

Post a Comment