提升图片上传、调整大小、存入数据库、显示
是否有一个简洁的示例来说明如何上传图像、调整其大小、将其存储在数据库中,然后使用 Lift 提供图像?
我确信我可以将文件上传、Java 2D API、Lift Mapper 和 Response API 拼凑在一起。但是有没有我可以遵循的示例代码以“正确”或推荐的方式进行操作?
Is there a succinct example of how to upload an image, resize it, store it in a database and then serve the image up using Lift?
I'm sure I could piece it together from the file upload, Java 2D API, Lift Mapper and Response APIs. But is there any example code I can follow to do it the 'correct' or recommended way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通过创建一个新的 MappedField 来为链接到 s3 的 Mapper 字段执行此操作。我还有一些要调整大小的代码,但尚未测试或部署(因此请谨慎使用)。
I did this for a Mapper field linked to s3 by creating a new MappedField. I also have a some code to resize, but haven't tested or deployed (so use with caution).
另一个答案很好地描述了如何调整图像大小并在文件系统上存储对文件的引用。
如果您想使用 lift 映射器来存储实际的文件内容,您必须创建自定义模型对象,并在其上定义一个二进制字段。尝试这样的操作:
然后,在引导类中,在末尾添加此
Document
:瞧。使用
文档保存(新文档)
将其存储到数据库中。可以使用set
方法设置新文档
的字段。尝试使用Document
单例的delete_!
、find
、findAll
方法来删除或在数据库中查找它。从现在开始应该很简单了。最后,要显示图像,您可以覆盖 Lift 的调度规则(在引导类 Boot.scala 中)。尝试使用以下示例,该示例会覆盖 pdf 请求的规则:
The other answer nicely describes how to resize the image and store a reference to the file on the file system.
If you want to use the lift mapper to store the actual file contents, you have to create your custom model object, and define a binary field on it. Try something like this:
Then, in bootstrap class, add this
Document
at the end:Voila. Using
Document save (new Document)
stores it into database. Anew Document
's fields can be set using theset
method. Try playing withdelete_!
,find
,findAll
methods of theDocument
singleton to delete or find it in the database. It should be straightforward from this point on.Finally, to display the image, you can override Lift's dispatching rules (in bootstrap class, Boot.scala). Try playing around with this example which overrides the rules for pdf requests:
根据乔恩霍夫曼接受的答案,我修复了错误。他的版本弄乱了纵横比(总是变成 1:1),因为数学在几个地方出现了偏差。此版本会调整大图片的大小直至适合,并尊重纵横比。
Based on the accepted answer by Jon Hoffman, I fixed the bugs. His version messes up the aspect ratio (it always becomes 1:1), because the math was off in a few spots. This version resizes big pictures until they fit, and respects the aspect ratio.