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
Process.Start: How to Get the Output
What Does the =≫ Operator Mean in a Property
Cleanest Way to Write Retry Logic
Most Efficient Way to Concatenate Strings
Read/Write 'Extended' File Properties (C#)
How to Find Out Which Process Is Locking a File Using .Net
How to Write Unicode Characters to the Console
Do Httpclient and Httpclienthandler Have to Be Disposed Between Requests
How to Give a C# Auto-Property an Initial Value
Best Way to Randomize an Array With .Net
Type Checking: Typeof, Gettype, or Is
Observablecollection Not Noticing When Item in It Changes (Even With Inotifypropertychanged)
How to Load Dll (Module Could Not Be Found Hresult: 0X8007007E)
How to Suspend Painting For a Control and Its Children
Asp.Net MVC 5 Culture in Route and Url
How to Read and Parse an Xml File in C#
Why Are Cdecl Calls Often Mismatched in the "Standard" P/Invoke Convention