我的目标是在生成文件后重定向页面,但下面的代码不生成文件,因为它重定向到另一个页面。如何在重定向后生成文件?是否可以在服务器端脚本(Suitelet)中执行此操作?
Suitelet SuiteScript 2.0:
response.writeFile({
file:loanPortFolio
});
//There are no redirect before, only generate file, but because i can't generate again ,
//I need to refresh the page to generate again. so i Add this line of code to redirect me to same page.
response.sendRedirect({
type: http.RedirectType.SUITELET,
identifier : 'customscript_loan_port_detailed_report',
id : 'customdeploy_loan_portf_detailed_report',
parameters: {
type : 'monthly'
}
});
//Result? I can't generate because it redirect to another page -_-
简单回答:你不能同时拥有:writefile和redirect:如果一个被执行,另一个将被“取消”。
您没有提供完整的代码,但我假设您正在使用套件中的表单,并且您确实为表单添加了一个提交按钮,并且在POST中您正在生成文件并使用writeFile。
如果是这种情况,那么实现目标的一种方法是,在生成文件之后,而不是使用writeFile,您可以在表单中添加HTML字段并将其值设置为:
'<a href=[url] target=_blank>Download file</a>'
with:[url]是您可以从文件对象(loanPortFolio)获取的链接。
这样,在提交之后,用户将看到链接,并且因为目标是'_blank',文件将在新的选项卡/窗口中生成,因此表单窗口将保持打开状态并且可以再次使用。