我在一个文件夹中有一个pdf文件,我想从xpage下载它。
通常情况下,这只是类似于以下的html:
<a href='file://10.1.0.2/folder1/myfile.pdf'>click and download</a>我在一个简单的html文件中使用这一行进行了测试。在我的Xpage中,我创建了一个计算字段(HTML显示),并添加了作为值。我看到了正确的链接悬停,但点击什么都没有发生。有什么问题吗?
丁烯
发布于 2013-12-14 22:03:18
最近,我以无头XPage的形式编写了一个下载的"servlet“,解决了这类问题。对于链接添加onclick事件:
sessionScope.put("filepath", file);
context.redirectToPage("/_download.xsp") _download页面具有beforeRenderResponse事件facesContext.responseComplete(),并在afterRenderResponse中调用一个java代码,该代码读取文件并写入输出流。就像这样:
if (sessionScope.containsKey("filepath")){
FileDownload.sendFile(sessionScope.filepath);
}java类:
public class FileDownload {
public static void sendFile(String filepath) {
File file = new File(filepath);
if (file.exists()) {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
HttpServletResponse response = (HttpServletResponse) ec.getResponse();
response.setContentType(MIME(filepath)); // figure out the type from extension or else
OutputStream out;
try {
// for the download dialog
response.setHeader("Content-disposition",
"attachment; filename*=utf-8''" + java.net.URLEncoder.encode(file.getName(), "UTF-8").replace("+", "%20"));
out = response.getOutputStream();
FileInputStream in = new FileInputStream(file);
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.flush();
} catch (IOException e) {
}
}
}
}不幸的是,对于非常大的文件(1Gb左右),它变得缓慢,也占用了大约两倍于文件大小的内存,但是我不确定我能在这里优化什么。我尝试在循环中调用out.flush(),但没有任何效果。
发布于 2013-12-12 15:58:47
文件系统通常不会在Domino中公开。HTML根目录是domino data/domino/html,您的文件需要位于下面的文件夹中。文件夹名不能在名称中包含.nsf。您可以在internet站点中配置文件夹。
发布于 2013-12-12 16:01:49
像你这样的接缝混在一起
10.1.0.2是一个ipadress,它将引用一个url服务器,然后您应该添加url的http: infront而不是文件:
如果要访问计算机上的本地文件,请编写file://c:\path\file.ext
但是浏览器对访问服务器共享上的文件有一些限制。
检查此链接work
https://stackoverflow.com/questions/20545579
复制相似问题