MVC 服务器文件下载

文件上传到服务器后下载

window.open   与window.location.href  对txt  或是pdf文件执行的操作是打开,而非下载

mvc controller 自带有如下方法


protected internal FileContentResult File(byte[] fileContents, string contentType);
protected internal FileStreamResult File(Stream fileStream, string contentType);
protected internal FilePathResult File(string fileName, string contentType);
protected internal virtual FileContentResult File(byte[] fileContents, string contentType, string fileDownloadName);
protected internal virtual FileStreamResult File(Stream fileStream, string contentType, string fileDownloadName);
//
// 摘要:
// 使用文件名、内容类型和文件下载名创建一个 System.Web.Mvc.FilePathResult 对象。
//
// 参数:
// fileName:
// 要发送到响应的文件的路径。
//
// contentType:
// 内容类型(MIME 类型)。
//
// fileDownloadName:
// 浏览器中显示的文件下载对话框内要使用的文件名。
//
// 返回结果:
// 文件流结果对象。
protected internal virtual FilePathResult File(string fileName, string contentType, string fileDownloadName);

调用以上方法,即可对任何类型文件进行下载,并可以重命名

public ActionResult DownLoad(string path)
{
    return File(path, "", "重命名.txt");
}

原文地址:https://www.cnblogs.com/soundcode/p/5098918.html