Carrierwave如何获得文件扩展名 [英] Carrierwave how to get the file extension

查看:168
本文介绍了Carrierwave如何获得文件扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个需要文件上传/下载的Ruby on Rails应用程序。对于上传部分,我使用了宝石载波,因为它非常易于使用和灵活。问题是:一旦我上传文件,我需要知道一些事情:即如果它是一个PDF而不是下载文件我显示它内联,同样的图像。如何获取文件扩展名,以及如何将发送文件发送给用户?确定文件的扩展名(我想挂载上传者的名字是'file'):

/ p>

  file = my_model.file.url 
扩展名= my_model.file.file.extension.downcase

然后准备mime和处置变量:

  disposition ='attachment'
mime = MIME :: Types.type_for(file).first.content_type

if%w {jpg png jpg gif bmp} .include? (extension)or extension ==pdf
disposition ='inline'
end



然后发送文件:

  send_file档案,:type => mime,:处置=>处置


I'm developing a Ruby on Rails application that requires file uploading/downloading. For the upload part i used the gem carrierwave since it's very easy to use and flexible. The problem is: once i uploaded the file, i need to know a few things: i.e. if it's a pdf instead of downloading the file i show it inline,and the same goes for an image. How do i get the file extension and how can i do it to send the file to a user?? Any feedback is appreciated Thanks!!

解决方案

Determine file extension (I suppose a name for mounted uploader is 'file'):

file = my_model.file.url
extension = my_model.file.file.extension.downcase

Then prepare mime and disposition vars:

disposition = 'attachment'
mime = MIME::Types.type_for(file).first.content_type

if %w{jpg png jpg gif bmp}.include?(extension) or extension == "pdf"
  disposition = 'inline'
end

(add other extensions if you want).

And then send the file:

send_file file, :type => mime, :disposition => disposition

这篇关于Carrierwave如何获得文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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