我正在尝试获取一个 .csv 文件,其中包含一些由 jlst 对象过滤的数据(其中包含一些下拉框的选定 ID)。这是ajax调用:
$('#moduleCSVReport').click(function () {
$('#progress').show();
$.ajax({
url: '/Reports/GetPageAccessLevelsCSV?obj=' + jlst,
dataType: "json",
type: "GET",
success: function (data) {
$('#progress').hide();
}
});
});
这是功能:
public ActionResult GetPageAccessLevelsCSV(string obj)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
ReportsSelectedItems cParams = jss.Deserialize<ReportsSelectedItems>(obj);
var locationIdFilter = (cParams.LocationId != 0) ? cParams.LocationId : 0;
var divisionIdFilter = (cParams.DivisionId != 0) ? cParams.DivisionId : 0;
IList<UserProfile> userProfiles = new List<UserProfile>();
userProfiles = Session.Query<UserProfile>().Where(n => n.Employee != null && ((n.Employee.Division.Id == cParams.DivisionId || cParams.DivisionId == 0) && (n.Employee.Location.Id == cParams.LocationId || cParams.LocationId == 0))).OrderBy(n => n.Employee.Name).ToList();
MemoryStream output = new MemoryStream();
StreamWriter writer = new StreamWriter(output, Encoding.UTF8);
writer.Write("Name,");
writer.Write("Team");
foreach(UserProfile user in userProfiles)
{
writer.Write(user.Employee.Name);
writer.Write(",");
writer.Write(HasAccessToModule((int)ModuleEnum.Team, user.Id).ToString());
writer.WriteLine();
}
writer.Flush();
output.Position = 0;
return File(output, "application/x-ms-excel", "test.csv");
}
我正在跟踪断点,文件似乎已成功生成。只是我在ajax调用中接收到文件后不知道如何显示。我认为必须有一种方法来处理文件(可能在成功事件中)。如果是这样,我该怎么做?谢谢。