Grails & vue实现文件下载

vue中实现文件下载其实很简单,直接给出超级链接即可。比如:

<a href="下载文件的链接">提示信息</a>

如果与Grails配合,相应的后台代码:

    def downLoadFile(params) {
        def result = []
        if (params.downLoadFileName) {
            def filename = params.downLoadFileName
            def sf = new File(filename)
            log.info "download: ${sf} -- ${filename}"
            if (sf.exists()) {
                log.info "begin download......"
                def fName = sf.getName()
                // 处理中文乱码
                def name = URLEncoder.encode(fName, "UTF-8");
                def response = getResponse()
                response.setHeader("Content-disposition", "attachment; filename=" + name)
                response.contentType = "application/x-rarx-rar-compressed"
                //response.contentType = ""

                def out = response.outputStream
                def inputStream = new FileInputStream(sf)
                byte[] buffer = new byte[1024]
                int i = -1
                while ((i = inputStream.read(buffer)) != -1) {
                    out.write(buffer, 0, i)
                }
                out.flush()
                out.close()
                inputStream.close()
            } else {
                log.error("文件不存在.")
            }
        } else {
            log.error("缺少downLoadFileName参数.")
        }
        return result
    }

其中下载文件的文件名(URL)通过参数downloadFileName给出。

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页