2014-11-03 286 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