如何在使用 Rails 上传时验证损坏的图像文件 [英] How to validate corrupt image file while uploading using Rails

查看:40
本文介绍了如何在使用 Rails 上传时验证损坏的图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在使用 Rails 上传时验证损坏的图像文件

  • 如果损坏的 jpg 或 png 文件(或将任何其他类型的文件的扩展名更改为 jpg)

    系统将不得不在上传过程中显示错误

解决方案

您可以使用 mime-types 库.

type = MIME::Types.type_for(your_file.original_filename).first

不过,我不确定这将如何处理损坏/损坏的文件.请务必查看 Paperclip 以上传文件,因为它非常容易设置和使用.>

How to validate corrupt image file while uploading using Rails

  • if the corrupted jpg or png file (or change the extension of any other type of file to jpg)

    the system will have to show error during upload

解决方案

You can check the MIME type to make sure that it's not file with a changed extension by using the mime-types library.

type = MIME::Types.type_for(your_file.original_filename).first

I'm not sure how that would work with corrupted/broken files though. Make sure to look into Paperclip for uploading files, as it's ridiculously easy to set up and use.

这篇关于如何在使用 Rails 上传时验证损坏的图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆