Download File of Any Type in ASP.NET MVC Using Fileresult

Download file of any type in Asp.Net MVC using FileResult?

You can just specify the generic octet-stream MIME type:

public FileResult Download()
{
byte[] fileBytes = System.IO.File.ReadAllBytes(@"c:\folder\myfile.ext");
string fileName = "myfile.ext";
return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
}

How To Download A file From FileResult and anchor tag in MVC 5?

Error Code 404 means the url is not found by your anchor tag. https://en.wikipedia.org/wiki/HTTP_404

You need to pass ImageName parameter from your anchor tag to the controller. You can do something like this:

View

@model IEnumerable<mallform.Models.FileUpload>
@{
ViewBag.Title = "Downloads";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Downloads</h2>


@foreach (var file in Model)
{

@Html.ActionLink(
"Download File", // Anchor Text
"Download", // Action Name
"Controller Name", // Controller Name
new {
ImageName= "Pass the value of imagename parameter"
},
null // Html Attributes
)
}

Controller

    public FileResult Download(string ImageName)
{
//var FileVirtualPath = "~/uploads/" + ImageName;

//If using Physical Path
//var FileVirtualPath = HttpContext.Current.Request.MapPath("~/uploads/" + ImageName);

//If using Virtual Path
var FileVirtualPath = HttpContext.Current.Server.MapPath("~/uploads/" + ImageName);

return File(FileVirtualPath, "application/force-download", Path.GetFileName(FileVirtualPath));

}

Returning a file to View/Download in ASP.NET MVC

public ActionResult Download()
{
var document = ...
var cd = new System.Net.Mime.ContentDisposition
{
// for example foo.bak
FileName = document.FileName,

// always prompt the user for downloading, set to true if you want
// the browser to try to show the file inline
Inline = false,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(document.Data, document.ContentType);
}

NOTE: This example code above fails to properly account for international characters in the filename. See RFC6266 for the relevant standardization. I believe recent versions of ASP.Net MVC's File() method and the ContentDispositionHeaderValue class properly accounts for this. - Oskar 2016-02-25

Download File in Asp.net MVC

The second parameter in any Controller.File should be the content type, yet is seems the code tries to pass both the type and the file name. Perhaps this is a typo and the intent was to type:

return File(doc.Data, MimeMapping.GetMimeMapping(doc.Name), doc.Name)

doc.Data should be either a byte array or a Stream to avoid confusion with the Controller.File(string,string,string) overload which expects the first argument to be the path to a server file.

Moreover, GetMimeMapping should return a valid MIME type like application\octet-stream or application/vnd.openxmlformats-officedocument.spreadsheetml.sheet for .xlsx files, etc.

The browser can open the correct program on the client by checking first the content type, then the file extension. This means that an Excel file can be sent as application\octet-stream but the target name (doc.Name in this case) must include the proper extension.

Finally, to ensure the Javascript code isn't messing anything up, either Fiddler or the Network capture functionality of Chrome or IE should be used to check exactly what is sent to the server and what is returned.

Download a file with mvc

Does it have to be done using ajax? Maybe you could open another window with the file generation address and let the browser do the job:

function downloadFile(id) {

window.open(sitePath + "Controller/DownloadFile?fileid=" + id, '_blank');

}


Related Topics



Leave a reply



Submit