gpt4 book ai didi

java - 在浏览器中预览本地 html 文件

转载 作者:搜寻专家 更新时间:2023-11-01 02:30:36 25 4
gpt4 key购买 nike

我有一个网页,用户可以在其中上传文件。他可以使用 <t:inputFileUpload> 选择要上传的文件jsf标签。(此标签类似于带有 type="file" 的标准 HTML 输入标签)。

我想添加预览功能。

当用户选择一个文件(例如:html 文件)时,他可以预览他的 html 本地文件。

我知道浏览器的安全限制原因存在“问题”。

我的第一个解决方案是将文件从用户本地计算机加载到服务器,然后在服务器上建立到该文件的链接。问题是我的服务器有风险,也许这个文件已损坏......

有人对这个 future 有想法和解决方案吗?

最佳答案

这是一个简单的 JSFiddle显示 html 文件的预览。我根据找到的教程创建了它 here .

代码中对您最有用的部分如下

var f = // uploaded file.
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Render the file.
$("#preview").html(e.target.result);
};
})(f);

// Read in the html file as text.
reader.readAsText(f);

这使用 HTML5 FileReader 读取和显示 html 文件的内容。

关于java - 在浏览器中预览本地 html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10399023/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com