grails 3加载图像外部文件夹

时间:2017-02-13 19:30:47

标签: java image grails resources asset-pipeline

我想从部署grails应用程序的同一服务器的外部文件夹中加载图像。让我们说一下linux - user home。

我在网上发现了很多文章但没有成功。

我的reinterpret_cast代码是尝试:

.gsp

我需要类似的东西:

<asset:image src="libs/test/1.jpg"  alt=""/>
<asset:image src="images/2.jpg"  alt=""/>
<img src="${asset.assetPath(src: '13123.jpg')}" />
<img src="\home\user\img\Desert.jpg" />

其中userhome是预定文件夹。

使用资产管道插件的另一个问题我得到了这个错误:

<img src="${userhome}/image.jpg" />

1 个答案:

答案 0 :(得分:1)

您无法使用资产标记指向外部文件。但是您可以从文件系统中检索文件并将其作为字节数组“附加”到响应中,作为控制器操作的示例:

firstName: data.firstName,
               ^      
TypeError: Cannot read property 'firstName' of undefined     

服务逻辑可能如下所示:

const express = require('express');
const Docxtemplater = require('docxtemplater');
const JSZip = require('jszip');
const fs = require('fs');
const path = require('path');
const bodyParser = require('body-parser');

function estateDoc (data) {

let content = fs.readFileSync(path.resolve(__dirname, 'docxGen.docx'), 'binary');

let zip = new JSZip(content);

let doc = new Docxtemplater();
doc.loadZip(zip);

doc.setData({
  firstName: data.firstName,
  lastName: data.lastName,
  middleName: data.middleName,
  suffix: data.suffix,
  socialSecurity: data.socialSecurity,
  address: data.address,
  telephone: data.telephone,
  heir: data.heir
});
  try {
      doc.render()
  }
  catch (error) {
      var e = {
          message: error.message,
          name: error.name,
          stack: error.stack,
          properties: error.properties,
      }
      console.log(JSON.stringify({error: e}));
      throw error;
  }
  var buf = doc.getZip()
               .generate({type: 'nodebuffer'});
  fs.writeFileSync(path.resolve(__dirname + '/doc-sender-catcher', 'output.docx'), buf)
};
estateDoc();

module.exports = {estateDoc};

最后是gsp上的 img 标记:

def getImage() {
    byte[] imageInBytes = imageService.getImage()
    response.with{
        setHeader('Content-length', imageInBytes.length.toString())
        contentType = 'image/jpg' // or the appropriate image content type
        outputStream << imageInBytes
        outputStream.flush()
    }
}

您可能希望使用ID和URL映射来检索使用服务逻辑的特定图像。