我在本地驱动器中创建了一个word文档,应该可以使用grails gsp页面在浏览器中打开它。
创建、创建链接或使用脚本的选项有哪些。提前感谢您的帮助。
发布于 2013-12-16 15:29:30
要打开或下载有很多选项
.gsp文件中提供链接,如下所示,并在使用以下代码按下该文档的链接时设置download或view option/open选项。在表格列表中显示从数据库或某些其他来源获取链接
<table>
<thread>
<tr>
<g:sortableColumn property="filename" title="Filename" />
<g:sortableColumn property="upload" title="Upload Date" />
</tr>
</thread>
<tbody>
<g:each in="${documentInstanceList}" status="i"
var="documentInstance">
<tr class="${(i%2)==0?'even':'odd'}">
<td><g:link action="download" id="${documentInstance.id}">
${documentInstance.filename}
</g:link></td>
<td><g:formatDate date="${documentInstance.uploadDate}" /></td>
</g:each>
</tbody>
</table>在您的DocumentController中,在download操作下放入此代码,以使文件可根据浏览器选项进行下载或查看
def download(long id) {
Document documentInstance = Document.get(id)
if (documentInstance == null) {
flash.message = "Document not found."
redirect(action:'list')
}
else {
response.setContentType("APPLICATION/OCTET-STREAM")
response.setHeader("Content-Disposition","Attachment;Filename=\"${documentInstance.filename}\"")
def file = new File(documentInstance.fullpath)
def fileInputStream = new FileInputStream(file)
def outputStream = response.getOutputStream()
byte[] buffer = new byte[4096];
int len ;
while((len = fileInputStream.read(buffer))>0) {
outputStream.write(buffer,0,len);
}
outputStream.flush()
outputStream.close()
fileInputStream.close()
}
}现在让我做任何事..。。。
发布于 2013-12-16 14:12:01
据我所知,没有任何插件可以让你在浏览器/ grails应用程序中直接呈现word文档。这在旧版本的Word和IE 7/8中是可能的,但现在不是这样了。
word文档基本上是一个XML文档,其中包含各种标记,告诉文本如何呈现等,您可以考虑加载此格式。以前可能有人这样做过,所以如果你想研究这个选项,我建议你搜索"word文档解析“或类似的东西。另一方面,如果您只对文档文本感兴趣,也许可以将word文档保存为txt?
希望这能有所帮助。
https://stackoverflow.com/questions/20604017
复制相似问题