【问题标题】:Regarding-Grails GSP关于-Grails GSP
【发布时间】:2013-12-16 05:24:14
【问题描述】:

我在本地驱动器中创建了一个 word 文档,它应该使用 grails gsp 页面在浏览器中打开。

创建链接或使用脚本创建的选项有哪些。 提前感谢您的帮助。

【问题讨论】:

    标签: grails


    【解决方案1】:

    要打开或下载有很多选项

    1. 是使用File Viewer Grails插件Grails File Plugin

    2. 只需在您的.gsp 文件中提供如下链接,并在您使用以下代码按下该文档的链接时创建downloadview 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 中,在下载操作下放置此代码,以根据浏览器选项使文件可供下载或查看

    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()
            }
        }
    

    现在什么都让我... .

    【讨论】:

      【解决方案2】:

      我不知道有任何插件可以让您直接在浏览器/grails 应用程序中呈现 word 文档。这在旧版本的 Word 和 IE 7/8 中曾经是可能的,但现在已经不是这样了。

      word 文档基本上是一个带有各种标记的 XML 文档,告诉文本如何呈现等,您可以考虑加载这种格式。以前可能有人这样做过,所以如果你想调查这个选项,我建议你用谷歌搜索“word 文档解析”或类似的东西。另一方面,如果您只对文档文本感兴趣,也许可以将 word 文档另存为 txt?

      希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-11-11
        • 1970-01-01
        • 1970-01-01
        • 2016-08-03
        • 1970-01-01
        • 2017-10-05
        • 1970-01-01
        相关资源
        最近更新 更多