【问题标题】:How to open resource in new window in vaadin?如何在vaadin的新窗口中打开资源?
【发布时间】:2013-02-11 12:33:45
【问题描述】:

我正在尝试在 vaadin 中生成 pdf。我的问题是生成的 pdf 在当前窗口(浏览器选项卡)中打开。我试过了:

 String filename = contentDataName + ".pdf";
    StreamResource resource = new StreamResource(source, filename, vaadinApplication);
    resource.getStream().setContentType("application/pdf");
    resource.getStream().setFileName(filename);
    resource.getStream().setParameter("Content-Disposition", "attachment; filename=\"" + filename + "\"");
    resource.getStream().setParameter("Content-Length", Integer.toString(fopOutput.size()));
    resource.setCacheTime(5000);
    resource.setMIMEType("application/pdf");

    mainWindow.open(resource);
    mainWindow.open(resource, "_blank", true);

它不起作用。我错过了什么?我也试过了

mainWindow.open(resource, "_blank");

【问题讨论】:

  • 我认为那里有一个错误,因为 getStream() 每次调用都会创建新的流。

标签: download window vaadin


【解决方案1】:

以下作品适合我 (Vaadin 7)

    ClickListener getHelpButtonListener(final Button helpButton)
{
log.debug("+++++++++ setButtonListener ...++++++++++++++===.........");
final ClickListener helpButtonlistener = new ClickListener()
{
    @Override
    public void buttonClick(ClickEvent event)
    {

    StreamSource source = new StreamSource()
    {
        public java.io.InputStream getStream()
        {
        try
        {
            String helpFilePath = basepath + "/datafiles/";
            File helpFile = new File(helpFilePath);
            FileInputStream helpFileInputStream = new FileInputStream(helpFile);
            return helpFileInputStream;
        } catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
        }
    };
    String filename = basepath + "/datafiles/help.pdf";
    StreamResource resource = new StreamResource(source, filename);
    resource.setMIMEType("application/pdf");
    resource.getStream().setParameter("Content-Disposition", "attachment; filename=" + filename);
    BrowserWindowOpener opener = new BrowserWindowOpener(resource);
    opener.extend(helpButton);
    }

};
return helpButtonlistener;
}

【讨论】:

    【解决方案2】:

    它应该这样工作。也许这可能会有所帮助:https://vaadin.com/forum/-/message_boards/view_message/1572816?

    【讨论】:

      【解决方案3】:

      我在我的项目中做了同样的工作,你可以在下面看到我的函数,它在 Vaadin 的窗口中显示“.pdf”。 (忽略对象和函数的奇怪命名,因为它只是我项目的代码的和平)

      private void publishReport(final String path){
          StreamSource s = new StreamSource() {
      
              public java.io.InputStream getStream() {
                  try {
                      File f = new File(path);
                      FileInputStream fis = new FileInputStream(f);
                      return fis;
                  } catch (FileNotFoundException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
                  return null;
              }
          };
      
          StreamSource ss = new StreamSource(){
      
      
              @Override
              public InputStream getStream() {
                  // TODO Auto-generated method stub
                  return null;
              }
      
          };
      
          StreamResource r = new StreamResource(s, "appointmentScheduleDate.pdf", app);
          r.setCacheTime(-1);
          Embedded e = new Embedded();
          e.setSizeFull();
          e.setType(Embedded.TYPE_BROWSER);
      
          r.setMIMEType("application/pdf");
      
          e.setSource(r);
          e.setHeight("650px");
          plaintReoprtWindow = new Window("Report");
          plaintReoprtWindow.center();
          plaintReoprtWindow.setModal(true);
          plaintReoprtWindow.setHeight("700px");
          plaintReoprtWindow.setWidth("900px");
          plaintReoprtWindow.addComponent(e);
          app.getMainWindow().addWindow(plaintReoprtWindow);
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-27
        • 1970-01-01
        • 2014-03-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多