開心生活站

位置:首頁 > 綜合知識 > 

mvc文件頁面怎麼寫

1.asp.net MVC 中文件下載的代碼怎麼寫,不要求上傳

控制器中寫一個Action,有直接返回File()類型的,該方法其實就是下載

mvc文件頁面怎麼寫

public ActionResult ExportFile()

{

Services.ImportAndExport manage = new Services.ImportAndExport();

string fileName = "abc.xls";//文件名

string filePath = "D:abc.xls";//文件路徑

string MIME = "application/vnd.ms-excel";//文件類型

return File(filePath, MIME, fileName);

}

2.MVC界面如何下載pdf文件

在頁面中直接打開PDF(要求機器已安裝adobe reader),則只需要修改HTTP標頭的參數:

將“Response.AddHeader("content-disposition", "attachment; filename=AdmissionTicket.pdf");”替換成“Response.AddHeader("content-disposition", string.Format("inline;filename={0}.pdf", admissionFormId));”

3.MVC2.0 如何下載文件

使用FileResult,注意指定合適的ContentType。

如下載ZIP文件:

var path = Server.MapPath("~/123.zip");

return File(path, "application/x-zip-compressed");

如果是word文檔

var path = Server.MapPath("~/123.doc");

return File(path, "application/msword");

你可以根據文件的後綴名來獲取ContentType:

4.剛接觸mvc,我需要把頁面顯示的內容導出到一個Excel文件

把導出做成一個單獨的form塊 在form中指定提交到哪個控制器中的哪個Action方法。

@using (Html.BeginForm()){ } 如果在BeginForm()沒有參數的情況下默認是提交到和這個文件名一樣的Action方法裏面。

@using (Html.BeginForm("Create", "FileUpload", FormMethod.Post, new { enctype = "multipart/form-data" })) {}

這是MVC3的form寫法。

在控制層寫一個方法 這個方法裏面在獲取一次你頁面顯示的數據然後做處理導入到Excel中.

標籤:mvc 文件 頁面