2011-10-01 32 views
6

tôi phải dữ liệu xuất khẩu để xem như Excel, Thật sự tôi đã thực hiện, nhưng nghi ngờ của tôi là khi sử dụngXuất khẩu File Excel để xem (MVC)

return new FileContentResult(fileContents, "application/vnd.ms-excel"); 

vs

return File(fileContents, "application/vnd.ms-excel"); 

và Cách đặt Tên tệp có thể tải xuống theo từng phương pháp này?

Ví dụ 1:

public ActionResult ExcelExport() 
{ 
    byte[] fileContents = Encoding.UTF8.GetBytes(data); 
    return new FileContentResult(fileContents, "application/vnd.ms-excel"); 
} 

Ví dụ: 2

public ActionResult ExcelExport() 
{ 
    byte[] fileContents = Encoding.UTF8.GetBytes(data); 
    return File(fileContents, "application/vnd.ms-excel"); 
} 

Trả lời

9

Bạn có thể đọc về sự khác biệt giữa FileContentResult & FileResult đây: What's the difference between the four File Results in ASP.NET MVC

Bạn có thể chỉ định tên tập tin như

này
return new FileContentResult(fileContents, "application/vnd.ms-excel") { FileDownloadName = "name.xls" }; 

// or 

// note that this call will return a FileContentResult object 
return new File(fileContents, "application/vnd.ms-excel", "name.xls"); 
Các vấn đề liên quan