是否可以从同一控制器返回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