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');
});
}
いいえ。それらは2つの異なるアクション呼び出しです。 – Nkosi
2件の結果を送信したいので、2件のリクエストが必要です。 2つの要求を行うためにアクションを呼び出すコードを変更する必要があります。 – glacasa
glacasaが言ったことは、$(document).readyにAjaxコールを作成し、2番目のアクションを取得するだけです。 – Valkyrie