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");
}
總結
以上是生活随笔為你收集整理的MVC 服务器文件下载的全部內容,希望文章能夠幫你解決所遇到的問題。