使用 grails 和谷歌应用程序引擎将图像存储为 blob 并动态存储视图
我正在尝试动态显示我作为 Blob 存储在 google 数据存储中的图像。我没有收到任何错误,但我在查看的页面上看到图像损坏。
任何帮助都会很棒!
我的 grails 应用程序
域类中有以下代码,具有以下内容
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
Long id
@Persistent
String siteName
@Persistent
String url
@Persistent
Blob img
@Persistent
String yourName
@Persistent
String yourURL
@Persistent
Date date
static constraints = {
id( visible:false)
}
我的控制器中的保存方法具有以下内容
def save = {
params.img = new Blob(params.imgfile.getBytes())
def siteInfoInstance = new SiteInfo(params)
if(!siteInfoInstance.hasErrors() ) {
try{
persistenceManager.makePersistent(siteInfoInstance)
} finally{
flash.message = "SiteInfo ${siteInfoInstance.id} created"
redirect(action:show,id:siteInfoInstance.id)
}
}
render(view:'create',model:[siteInfoInstance:siteInfoInstance])
}
我的视图具有以下内容
<img src="${createLink(controller:'siteInfoController', action:'showImage', id:fieldValue(bean:siteInfoInstance, field:'id'))}"></img>
,并且它在控制器中调用以显示图像链接的方法如下所示
def showImage = {
def site = SiteInfo.get(params.id)// get the record
response.outputStream << site.img // write the image to the outputstream
response.outputStream.flush()
}
I am trying to dynamically display an image that I am storing in the google datastore as a Blob. I am not getting any errors but I am getting a broken image on the page that I view.
Any help would be awesome!
I have the following code in my grails app
domain class has the following
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
Long id
@Persistent
String siteName
@Persistent
String url
@Persistent
Blob img
@Persistent
String yourName
@Persistent
String yourURL
@Persistent
Date date
static constraints = {
id( visible:false)
}
My save method in the controller has this
def save = {
params.img = new Blob(params.imgfile.getBytes())
def siteInfoInstance = new SiteInfo(params)
if(!siteInfoInstance.hasErrors() ) {
try{
persistenceManager.makePersistent(siteInfoInstance)
} finally{
flash.message = "SiteInfo ${siteInfoInstance.id} created"
redirect(action:show,id:siteInfoInstance.id)
}
}
render(view:'create',model:[siteInfoInstance:siteInfoInstance])
}
My view has the following
<img src="${createLink(controller:'siteInfoController', action:'showImage', id:fieldValue(bean:siteInfoInstance, field:'id'))}"></img>
and the method in my controller that it is calling to display a link to the image looks like this
def showImage = {
def site = SiteInfo.get(params.id)// get the record
response.outputStream << site.img // write the image to the outputstream
response.outputStream.flush()
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否从绑定中返回了正确的 byte[] ? IIRC,grails 在应用程序引擎上的字节数组和文件方面存在一些问题,因为它使用基于文件的多部分解析器,该解析器不符合 JRE 黑名单。
另请尝试在响应中返回内容类型:
response.contentType = "image/png";
are you getting back a correct byte[] from your binding? IIRC, grails has some problems with byte arrays and files on the app engine because it uses a file-based multipart resolver that is not kosher with the JRE blacklist.
Also try returning a content type on your response:
response.contentType = "image/png";