2011-10-01 31 views
6

tengo para exportar datos para ver como Excel, de hecho me han puesto en práctica, pero mi duda es cuando de usarExportación de archivo de Excel a Ver (MVC)

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

vs

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

y ¿Cómo se puede configurar el nombre de archivo descargable en cada uno de estos métodos?

Ejemplo 1:

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

Ejemplo: 2

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

Respuesta

9

Usted puede leer acerca de las diferencias entre FileContentResult & FileResult aquí: What's the difference between the four File Results in ASP.NET MVC

puede especificar el nombre de fichero así

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"); 
Cuestiones relacionadas