如何在新标签页中打开PDF文件 [英] How to open a PDF file in a new tab

查看:527
本文介绍了如何在新标签页中打开PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSF,并且我想在单击按钮时在新选项卡中打开PDF文件.

I am working with JSF and I want to open a PDF file in a new tab when I click on a button.

XHTML

<p:commandButton onclick="this.form.target = '_blank'"
                 actionListener="#{managedBean.openFile(file)}"
                 ajax="false" />

托管豆

public void openFile( File file ) {
FacesContext facesContext = FacesContext.getCurrentInstance();
        ExternalContext externalContext = facesContext.getExternalContext();
        HttpServletResponse response = (HttpServletResponse) externalContext.getResponse();
        BufferedInputStream input = null;
        BufferedOutputStream output = null;

        try {
            // Open file.
            input = new BufferedInputStream(new FileInputStream(file), 10240);

            // Init servlet response.
            response.reset();
            // lire un fichier pdf
            response.setHeader("Content-type", "application/pdf"); 
            response.setContentLength((int)file.length());

            response.setHeader("Content-disposition", "attachment; filename=" + node.getNomRepertoire());
            response.setHeader("pragma", "public");
            output = new BufferedOutputStream(response.getOutputStream(), 10240);

            // Write file contents to response.
            byte[] buffer = new byte[10240];
            int length;
            while ((length = input.read(buffer)) > 0) {
                output.write(buffer, 0, length);
            }

            // Finalize task.
            output.flush();
        } finally {
            // Gently close streams.

                output.close();
                input.close();
        }
}

问题是,当我使用此方法时,它仅下载文件.供您参考,我正在关注该帖子: http://balusc.omnifaces. org/2006/05/pdf-handling.html

The problem is when I use this method it only downloads the file. For your information, I'm following this post: http://balusc.omnifaces.org/2006/05/pdf-handling.html

推荐答案

您应该更改

response.setHeader("Content-disposition", "attachment; filename=" + node.getNomRepertoire());

进入

response.setHeader("Content-disposition", "inline; filename=" + node.getNomRepertoire());

因此,请使用inline代替attachment.

另请参阅:

这篇关于如何在新标签页中打开PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆