是否可以從同一控制器返回FileContentResult和PartialView?我無法讓它工作。主控制器代碼:MVC C#在返回PartialView之前從控制器返回文件
...
var generateClass = new GenerateExcel(); // create obj of another class
generateClass.Generate(reports); // generate .xlsx file and save it to server disk
Download(); // ??? download file to client PC via "Save as.." dialog
return PartialView("_PartialReport", reports); // second (main) return and the end of controller
下載()方法在這裏:
public FileContentResult Download()
{
using (HostingEnvironment.Impersonate())
{
byte[] doc = System.IO.File.ReadAllBytes(@"C:\temp\BLP.xlsx");
// doc is OK, it's size == size of .xlsx file
return File(doc, "application/vnd.ms-excel");
}
}
沒有錯誤,但不會工作。 幫助別人嗎?
更新:AJAX代碼示例
// Generate report by creation date
function ConstructReportByDate() {
var date1 = $('#DateFrom').val();
var date2 = $('#DateTo').val();
$.ajax({
url: '/Reports/ConstructReport',
type: "POST",
contentType: 'application/json; charset=utf-8',
cache: false,
data: '{"kind":"byDate", "date1":"' + date1 + '", "date2":"' + date2 + '"}'
})
.done(function (data) {
$('#Report').html(data);
})
.fail(function (xhr) {
alert('errorHere');
});
}
沒有。這些是兩個不同的行動電話。 – Nkosi
你想發送2個結果,所以這需要2個請求。您需要修改調用該操作的代碼以發出2個請求。 – glacasa
什麼glacasa說,只需在$(document).ready上創建一個Ajax調用,並獲得第二個動作。 – Valkyrie