I'm trying to add button to Users Maintenance and on button's click download excel file, containing some data. I have created PXAction and it's method as above:
我正在尝试向用户维护和按钮的单击下载excel文件添加按钮,其中包含一些数据。我创建了PXAction,它的方法如上:
public PXAction<Users> getUsers;
[PXUIField(DisplayName = "Get Users", MapEnableRights = PXCacheRights.Select, MapViewRights = PXCacheRights.Select,Visible = true), PXButton(CommitChanges = true)]
public IEnumerable GetUsers(PXAdapter adapter)
{
var accessByRoles = PXSelect<RolesInGraph>.Select(this.Base);
var usersByRole = PXSelect<UsersInRoles>.Select(this.Base);
var dt = GetTable();//GetTable returns some DataTable just for test now
XLWorkbook workbook = new XLWorkbook();
workbook.Worksheets.Add(dt, "UserAccessRigths");
using (MemoryStream MyMemoryStream = new MemoryStream())
{
workbook.SaveAs(MyMemoryStream);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=\"UserAccessRigths.xlsx\"");
HttpContext.Current.Response.AppendHeader("Content-Length", MyMemoryStream.ToArray().Length.ToString());
HttpContext.Current.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
HttpContext.Current.Response.BinaryWrite(MyMemoryStream.ToArray());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
return null;
}
Everything work but the part where the download in the browser must start. I'm getting as response the excel, but it's not being downloaded. Here is the response I'm getting in the browser:
一切正常,但浏览器中必须开始下载的部分。我得到了excel的响应,但它没有被下载。这是我在浏览器中得到的响应:
I will be very grateful if someone can help me.
Thanks in advance
如果有人可以帮助我,我将非常感激。提前致谢
1 个解决方案
#1
5
Try PXRedirectToFileException to redirect the user browser to the Excel file. Default behavior from mainstream browser is to detect Excel mime type by extension and initiate a download. Second parameter of PXRedirectToFileException is used to force download.
尝试使用PXRedirectToFileException将用户浏览器重定向到Excel文件。主流浏览器的默认行为是通过扩展检测Excel mime类型并启动下载。 PXRedirectToFileException的第二个参数用于强制下载。
throw new PXRedirectToFileException(new PX.SM.FileInfo(Guid.NewGuid(),
"UserAccessRigths.xlsx",
null,
MyMemoryStream.ToArray()),
true);
#1
5
Try PXRedirectToFileException to redirect the user browser to the Excel file. Default behavior from mainstream browser is to detect Excel mime type by extension and initiate a download. Second parameter of PXRedirectToFileException is used to force download.
尝试使用PXRedirectToFileException将用户浏览器重定向到Excel文件。主流浏览器的默认行为是通过扩展检测Excel mime类型并启动下载。 PXRedirectToFileException的第二个参数用于强制下载。
throw new PXRedirectToFileException(new PX.SM.FileInfo(Guid.NewGuid(),
"UserAccessRigths.xlsx",
null,
MyMemoryStream.ToArray()),
true);