2014-11-03 243 views
3

我正在使用MVC应用程序。我想用Jquery AJAX下载excel文件和PDF文件。如何下载Excel文件和PDF文件使用JQuery Ajax MVC

在浏览网页

jQuery的AJAX

$.ajax({ 
     type: 'GET', 
     url: '/Report/ExportReports', 
     contentType:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", 
     data: { 
      Parameter1: Parameter1, 
      Parameter2: Parameter2, 
     }, 
     cache: false, 
     success: function (isSuccess) { 
      if (isSuccess.Success) { 
       } 
      } else { 
       alert('Something went wrong. Please try again after sometime...'); 
      } 
     }, 
     error: function (data, status, e) { 
     } 
    }); 

在控制器

public ActionResult ExportReports(string Parameter1, string Parameter2) 
    { 
     if (Parameter1 = "PDF") 
     { 
      DataTable exportData = grid.GetExportData(dataSource); 
      MemoryStream pdfStream = gridData.ExportToPDF(exportData, repType); 

      Response.ClearContent(); 
      Response.ClearHeaders(); 
      Response.ContentType = "application/pdf"; 
      Response.AppendHeader("Content-Disposition", "attachment; filename=" + executeRepType + ".pdf"); 
      Response.BinaryWrite(pdfStream.ToArray()); 
      Response.End(); 
     } 
     else 
     { 
      DataTable exportData = grid.GetExportData(dataSource); 
      MemoryStream excelStream = gridData.ExportToExcel(exportData, executeRepType); 
      //Write it back to the client 
      Response.ClearContent(); 
      Response.ClearHeaders(); 
      Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
      Response.AddHeader("content-disposition", "attachment; filename=" + executeRepType + ".xlsx"); 
      Response.BinaryWrite(excelStream.ToArray());//.GetAsByteArray()); 
      Response.End(); 
     } 
     return View(); 
    } 

所以在控制我们得到的所有数据,但我们不能够返回到视图页面。

+0

你为什么需要ajax? – charlietfl 2014-11-03 12:16:59

回答

3

您可以试试这个解决方案。 在查看页面

@Html.ActionLink("Export To Excel", "ExportReports", new { isPdfExport = false,Parameter1="_Parameter1" ,Parameter2="_Parameter2"}, new { @class="excelbtn" }) 

@Html.ActionLink("Export To PDF", "ExportReports", new { isPdfExport = true,Parameter1="_Parameter1" ,Parameter2="_Parameter2"}, new { @class="pdfbtn" }) 
如果要更改参数1和参数2的值动态比你可以使用JavaScript的描述如下

在Javascript中

: -

$('.excelbtn').attr('href', function() { 
     return this.href.replace('_Parameter1', Value1).replace('_Parameter2',Value2); 
      }); 
$('.pdfbtn').attr('href', function() { 
     return this.href.replace('_Parameter1', Value1).replace('_Parameter2',Value2); 
      }); 

在控制器: -

public ActionResult ExportReports(bool isPdfExport,string Parameter1, string Parameter2) 
{ 
    if (Parameter1 = "PDF") 
    { 
     DataTable exportData = grid.GetExportData(dataSource); 
     MemoryStream pdfStream = gridData.ExportToPDF(exportData, repType); 

     Response.ClearContent(); 
     Response.ClearHeaders(); 
     Response.ContentType = "application/pdf"; 
     Response.AppendHeader("Content-Disposition", "attachment; filename=" + executeRepType + ".pdf"); 
     Response.BinaryWrite(pdfStream.ToArray()); 
     Response.End(); 
    } 
    else 
    { 
     DataTable exportData = grid.GetExportData(dataSource); 
     MemoryStream excelStream = gridData.ExportToExcel(exportData, executeRepType); 
     //Write it back to the client 
     Response.ClearContent(); 
     Response.ClearHeaders(); 
     Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
     Response.AddHeader("content-disposition", "attachment; filename=" + executeRepType + ".xlsx"); 
     Response.BinaryWrite(excelStream.ToArray());//.GetAsByteArray()); 
     Response.End(); 
    } 
    return View(); 
} 
0

我会建议你在开箱即用的工具的帮助下更简单一点。 System.Web.MVC.Controller.File为您提供了一种方法,它将使用字节数组或流或文件路径来完成您所需的任务。因此,而不是这部分(和同为PDF)

Response.ClearContent(); 
Response.ClearHeaders(); 
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
Response.AddHeader("content-disposition", "attachment; filename=" + executeRepType + ".xlsx"); 
Response.BinaryWrite(excelStream.ToArray());//.GetAsByteArray()); 
Response.End(); 

我会用这样的

File(excelStream.ToArray(), System.Net.Mime.MediaTypeNames.Application.Octet, executeRepType + ".xlsx"); 

并且在异步请求没有实际需要。所以你可以直接使用链接。

+0

我正在使用类似这样的东西,但是如何在此之后更新视图(或刷新页面)?出于某种原因,即使没有'Response.End()',页面在我写入文件后也会停止处理。 – Danicco 2014-11-06 17:50:26

+1

取决于你在前端使用的是几种解决方案。经典之一是使用[@ Ajax.ActionLink](http://tinyurl.com/obawefg)与'OnSuccess' [AjaxOption](http://tinyurl.com/m7h7ndy),您可以在其中设置JavaScript函数,它将在控制器巧妙地处理你的请求后执行。作为替代方案,您可以使用[@ Html.ActionLink](http://tinyurl.com/mgz33gc)并为其指定id或class attr,例如(@文件)''('#file')。点击(函数()函数) {$。员额(this.href ..)})' – Valerii 2014-11-06 21:56:33