【问题标题】:How to access files in FileSystemStorage in Browser component如何在浏览器组件中访问 FileSystemStorage 中的文件
【发布时间】:2020-08-16 09:46:35
【问题描述】:

我试图在浏览器组件内显示保存在 FileSystemStorage 中的 pdf,但它一直在控制台上给我这个错误:[0813/072549.347989:INFO:CONSOLE(37)] "Not allowed to load local resource: file://home//Chapter_11.pdf#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0&view=FitH", source: https://cn1app/streams/1 (37) [0813/072551.123557:INFO:CONSOLE(0)] "Not allowed to load local resource: file://home//Chapter_11.pdf#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0&view=FitH", source: https://cn1app/streams/1 (0) 。好像有一个限制,我需要解决。我该如何解决这个问题?

编辑...在下面添加了信息。

这是我正在使用的代码:

Form hi;
Container loadingMsg;

public void Home(){
    Form form = new Form("Display pdf", new BorderLayout());
    
    Button showPdf = new Button("Show Pdf");
    showPdf.addActionListener(l->{
        show();
    });
    
    form.add(BorderLayout.CENTER, showPdf);
    form.show();
}

private void show(){
 
    hi = new Form("PDF Viewer", new BorderLayout());
    Label loadingLabel = new Label("Loading PDF...");
    loadingMsg = FlowLayout.encloseCenter(loadingLabel);
    hi.add(BorderLayout.NORTH, loadingMsg);
    
    String pdfUrl = "https://as.vanderbilt.edu/chemistry/Rizzo/chem220a/Chapter_11.pdf";
    String fileName = FileSystemStorage.getInstance().getAppHomePath() + "Chapter_11";
    
    if (!FileSystemStorage.getInstance().exists(fileName)) {
        Util.downloadUrlToFileSystemInBackground(pdfUrl, fileName);
    };
   
    hi.addShowListener(l -> {
        run1(fileName);
    });
    hi.show();
}

private void run1(String fileName) {
    BrowserComponent browser = new BrowserComponent();
    browser.setPage(getPdfViewerHtml(fileName), null);
    hi.add(BorderLayout.CENTER, browser);
    loadingMsg.remove();
    hi.revalidate();

}

private String getPdfViewerHtml(String fileName) {
      String html = "<!DOCTYPE html>\n"
            + "<html>\n"
            + "  <head>\n"
            + "    <title>PDF Viewer</title>\n"
            + "    <style>\n"
            + "    html{\n"
            + "      height: 100%;\n"
            + "      padding: 0;\n"
            + "    }\n"
            + "    body{\n"
            + "      height: 100%;\n"
            + "      overflow-y: hidden;\n"
            + "      position: fixed;\n"
            + "      width: 100%;\n"
            + "      padding: 0;\n"
            + " margin: 0;\n"
            + "    }\n"
            + "    </style>\n"
            + "  </head>\n"
            + "  <body>\n"
            + "\n"
            + "    <div style= \"height: 100%; margin: 0;\">\n"
            + "        <iframe\n"
            + "          src='"+fileName+"#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0&view=FitH'\n"
            + "          width=\"100%\"\n"
            + "          height=\"100%\"\n"
            + "        >\n"
            + "        <p>This browser does not support PDF!</p>\n"
            + "        </iframe>\n"
            + "\n"
            + "    </div>\n"
            + "\n"
            + "  </body>\n"
            + "</html>";
    return html;
}

所以在 String getPdfViewerHtml(String fileName) 方法中,当我用 URL 替换 fileName 时,一切正常。但我希望它显示来自 FileSystemStorage 的文件。

【问题讨论】:

  • 你是如何尝试加载它的?这可能是由于浏览器中的同源限制。这是在模拟器上吧?请说明在这种情况下您是使用 CEF 还是 FX codenameone.com/blog/big-changes-jcef.html
  • 我正在使用 CEF。
  • @ShaiAlmog 你的意思是可以在不使用cn1lib github.com/shannah/CN1Webserver 的情况下访问BrowserComponent 内的FileSystemStorage 中的文件吗?此外,我们如何调试 BrowserComponent 内部发生的错误?比如,如果真的是你想象的CORS问题,或者其他类型的问题,我们怎么能看到内部的浏览器控制台呢?是不是要看原生的日志,还是可以通过CN1拦截记录的错误?
  • @RichardMatovu 你没有回答我评论中的其他问题吗?
  • @FrancescoGalgani 您可以完全从文件系统加载 HTML 层次结构。我们支持将 HTML 代码放在 html 根包中,您可以将其用作文件系统 Web UI。仅在文件系统浏览器中无法正常工作的情况下才需要网络服务器,这是一种边缘情况

标签: codenameone


【解决方案1】:

下面的代码应该正确支持本地嵌入标签:

    private Container loadingMsg;

    public void start() {
        if (current != null) {
            current.show();
            return;
        }
        home();
    }


    public void home() {
        Form form = new Form("Display pdf", new BorderLayout());

        Button showPdf = new Button("Show Pdf");
        showPdf.addActionListener(l -> {
            showPdf.setText("Dowloading PDF...");
            showPdf.setEnabled(false);
            form.revalidate();
            show();
        });

        form.add(BorderLayout.CENTER, showPdf);
        form.show();
    }

    private void show() {
        String root = getAppHomePath() + "httpdocs/";
        mkdir(root);
        
        hi = new Form("PDF Viewer", new BorderLayout());
        Label loadingLabel = new Label("Loading PDF...");
        loadingMsg = FlowLayout.encloseCenter(loadingLabel);
        hi.add(BorderLayout.NORTH, loadingMsg);

        String pdfUrl = "https://as.vanderbilt.edu/chemistry/Rizzo/chem220a/Chapter_11.pdf";
        String fileName = root + "/Chapter_11.pdf";

        if (!FileSystemStorage.getInstance().exists(fileName)) {
            Util.downloadUrlToFile(pdfUrl, fileName, false);
            try {
                run1(docRoot, "Chapter_11.pdf");
            } catch (IOException ex) {
                Log.e(ex);
            }
        } else {
            try {
                run1(docRoot, fileName);
            } catch (IOException ex) {
                Log.e(ex);
            }
        }

        hi.show();
    }

    private void run1(String docRoot, String fileName) throws IOException {
        
        BrowserComponent browser = new BrowserComponent();
        String localUrl = fileName;
        String htmlPage = getPdfViewerHtml(localUrl);
        File indexHtml = new File(docRoot, "index.html");
        writeStringToFile(indexHtml, htmlPage);
        browser.setURL(docRoot + "/index.html");
        hi.add(BorderLayout.CENTER, browser);
        loadingMsg.remove();
        hi.revalidate();

    }

    private String getPdfViewerHtml(String fileName) {
        String html = "<!DOCTYPE html>\n"
                + "<html>\n"
                + "  <head>\n"
                + "    <title>PDF Viewer</title>\n"
                + "    <style>\n"
                + "    html{\n"
                + "      height: 100%;\n"
                + "      padding: 0;\n"
                + "    }\n"
                + "    body{\n"
                + "      height: 100%;\n"
                + "      overflow-y: hidden;\n"
                + "      position: fixed;\n"
                + "      width: 100%;\n"
                + "      padding: 0;\n"
                + " margin: 0;\n"
                + "    }\n"
                + "    </style>\n"
                + "  </head>\n"
                + "  <body>\n"
                + "\n"
                + "    <div style= \"height: 100%; margin: 0;\">\n"
                + "        <iframe\n"
                + "          src='" + fileName + "#toolbar=0&navpanes=0&scrollbar=0&statusbar=0&messages=0&scrollbar=0&view=FitH'\n"
                + "          width=\"100%\"\n"
                + "          height=\"100%\"\n"
                + "        >\n"
                + "        <p>This browser does not support PDF!</p>\n"
                + "        </iframe>\n"
                + "\n"
                + "    </div>\n"
                + "\n"
                + "  </body>\n"
                + "</html>";
        return html;
    }
    
    private void writeStringToFile(File file, String content) throws IOException {
        FileSystemStorage fs = FileSystemStorage.getInstance();
        try (OutputStream os = fs.openOutputStream(file.getAbsolutePath())) {
            Util.copy(new ByteArrayInputStream(content.getBytes("UTF-8")), os);
        }
        
    }

}

【讨论】:

  • 使用更具体的文件名编辑了您的答案并删除了一些样板
  • 效果很好。它显示来自系统存储的文件;它在模拟器上完美运行,但在手机上(在 Android 上测试),它不显示。它只是显示一个白屏。
  • @ShaiAlmog 在这部分 String root = getAppHomePath() + "httpdocs/"; mkdir(root); 如果根存在,它不会被替换并且它的所有内容都丢失了吗?
  • 没有。它只会尝试创建目录。如果它存在,它不会做任何事情
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-15
  • 2015-11-10
  • 1970-01-01
  • 2019-06-19
相关资源
最近更新 更多