我有一个Wicket应用程序,允许用户生成PDF,并在弹出窗口中打开它们。我这样定义了我自己的共享资源
public class PdfResourceReference extends SharedResourceReference {
@Override
public IResource getResource() {
return new ByteArrayResource("application/pdf") {
@Override
protected byte[] getData(final Attributes attributes) {
// generate the pdf and return byte[]
}
};
}
}在应用程序类中,我通过
mountResource("reportPdf", new PdfResourceReference());一切正常,浏览器会打开pdf文件。但问题是,pdf文件的名称(一旦用户尝试保存)总是"reportPdf“。我们的用户希望根据报告类型或客户编号来命名pdf。像"0123someCustomerId_report.pdf“这样的东西
我找到了一个similar stackoverflow question,它建议使用"Content-disposition“头。不幸的是,我不知道如何让它工作(似乎并不是所有的浏览器都支持它)。
对于这类问题,还有其他解决方案吗?可以将挂载路径与动态(类似正则表达式的)路径一起使用吗?
发布于 2014-03-17 21:18:47
我猜你有一个带有href="reportPdf"的静态锚点。
请改用DownloadLink。在这里,您可以使用DownloadLink(String id, IModel<File> model, String fileName)指定文件名从IModel返回PDF,最好是LoadableDetachableModel。
很大程度上是这样的:How to use Wicket's DownloadLink with a file generated on the fly?
发布于 2014-03-17 22:29:51
我通过向挂载的URL提供"filename“参数来解决这个问题:
mountResource("reportPdf/${filename}", new PdfResourceReference());向PageParameters添加一个"filename“参数,然后生成像/reportPdf/0123omeCustomerIdReport这样的urls,浏览器以用户喜欢的方式保存它。
也许有人想出了一个更好的解决方案,但到目前为止,它工作得很好,我不必摆弄每个浏览器都会有不同解释的HTTP头。
https://stackoverflow.com/questions/22454024
复制相似问题