我在我的Home控制器中创建了一个FileResult方法,该方法接受FormCollection并根据输入返回一个文件。该方法可以很好地获取数据,因为在检查Fiddler之后,Image选项将作为图像返回。 这是被称为
的控制器方法public FileResult DownloadReport(FormCollection collection)
{
byte[] response;
string fileName = "";
string contentType = "";
string startDate = Convert.ToString(collection["hidden-start-date"]);
string endDate = Convert.ToString(collection["hidden-end-date"]);
string storeID = Convert.ToString(collection["hidden-store-id"]);
string reportID = Convert.ToString(collection["hidden-report-id"]);
string returnType = Convert.ToString(collection["hidden-return-type"]);
DateTime dateStart = DataConversion.getDateTimeFromStringDateResponse(startDate.ToString());
DateTime dateEnd = DataConversion.getDateTimeFromStringDateResponse(endDate.ToString());
if (returnType == "pdf")
{
fileName = "report.pdf";
contentType = "application/pdf";
}
if (returnType == "image")
{
fileName = "report.tif";
contentType = "image/tiff";
}
if (returnType == "excel")
{
fileName = "report.xlsx";
contentType = "application/vnd.ms-excel";
}
if (returnType == "word")
{
fileName = "report.docx";
contentType = "application/msword";
}
SynchDB.User returnObject = (SynchDB.User)Session["SessionUserReturnObject"];
response = SoapServiceController.getExportReportResponse(returnObject.AccountLogin, returnObject.AccountPassword, returnObject.CompanyID, Convert.ToInt64(storeID), 0, returnObject.ID, Convert.ToInt64(reportID), dateStart, dateEnd, returnType);
Response.AppendHeader("Content-Disposition", "inline; filename=" + fileName);
return File(response, contentType);
}
调用该方法的表单如下所示,它位于_Layout.cshtml文件中
<form id="form-content-type" method="post" action="@Url.Action("DownloadReport","Home")">
<input type="hidden" id="hidden-report-id" name="hidden-report-id" />
<input type="hidden" id="hidden-store-id" name="hidden-store-id" />
<input type="hidden" id="hidden-start-date" name="hidden-start-date" />
<input type="hidden" id="hidden-end-date" name="hidden-end-date" />
<input type="hidden" id="hidden-return-type" name="hidden-return-type" />
</form>
调用该方法的javascript看起来像这样
downloadReport: function () {
$.mobile.loading('show');
$('#hidden-report-id').val(storage.readValue('reportID'));
$('#hidden-store-id').val(storage.readValue('storeID'));
$('#hidden-start-date').val(storage.readValue('startDate'));
$('#hidden-end-date').val(storage.readValue('endDate'));
$('#hidden-return-type').val(storage.readValue('returnType'));
$('#form-content-type').submit();
}
和
$('#pdf-download-button').click(function () {
$('#downloadPopup').popup('close');
storage.storeValue("returnType", "pdf");
DataAccess.downloadReport();
})
每次调用该方法时,它都会返回默认视图,该视图是主控制器的默认路径,而不是返回文件。
非常感谢任何帮助,谢谢