【发布时间】: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