我有一个使用EPPlus在服务器上生成的Excel文件,文件正确生成,在本地机器上使用window.location可以正常工作,但在部署到服务器时却无法工作。我试图通过MVC控制器返回一个FileStreamResult,但我认为它没有起作用。我正在使用ajax调用来访问控制器方法,但当该方法运行时,它无法进入.done。
我一直在参考ASP.NET MVC EPPlus Download Excel File进行C#编程。
什么是获取该文件的最简单方法?
我一直在参考ASP.NET MVC EPPlus Download Excel File进行C#编程。
function exportToExcel() {
var batchName = $("#batchDateSelect option:selected").text();
var bID = $("#batchDateSelect").val();
var params = {
BatchID: bID,
BatchName: batchName
};
$.post(path + "Export/ExportToExcel", params)
.done(function (Data, textStatus, jqXHR) {
var fileName = "";
////window.location = path + "ExportFiles/"+fileName;
});
}
控制器
public ActionResult ExportToExcel(int BatchID,string BatchName)
{
FileStreamResult FSR = DataAccess.ExportUtility.CreateExcelFile(BatchID, BatchName);
return FSR;
}
< p > EPPlus 方法
public static FileStreamResult CreateExcelFile(int batchid,string batchName)
{
string fileName = batchName + " Reason_Code_Export.xlsx";
var serverPath = HttpContext.Current.Server.MapPath("~/ExportFiles/");
DirectoryInfo outputDir = new DirectoryInfo(serverPath);
FileInfo newfile = new FileInfo(outputDir.FullName + fileName);
if (newfile.Exists)
{
newfile.Delete();
newfile = new FileInfo(outputDir.FullName + fileName);
}
Dictionary<string,int> MAData = PolicyDataAccess.GetMatchActionData(batchid);
MemoryStream MS = new MemoryStream();
using (ExcelPackage package = new ExcelPackage(newfile))
{
..........
........
package.SaveAs(MS);
}
MS.Position = 0;
var contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
FileStreamResult FSR = new FileStreamResult(MS, contentType);
FSR.FileDownloadName = fileName;
return FSR;
}
什么是获取该文件的最简单方法?