Carrierwave如何获取文件扩展名

我正在开发一个需要文件上传/下载的Ruby on Rails应用程序。 对于上传部分,我使用了gem carrierwave,因为它非常易于使用且灵活。 问题是:一旦我上传了文件,我需要知道一些事情:即如果它是pdf而不是下载文件我将其显示为内联,并且图像也是如此。 我如何获得文件扩展名 ,我该怎么做才能将文件 发送给用户? 任何反馈表示赞赏谢谢!!

确定文件扩展名(我假设已安装的上传者的名称是’file’):

file = my_model.file.url extension = 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 file, :type => mime, :disposition => disposition 

上传文件后,名称将存储在数据库中。 这还包括扩展。 假设你有一个User模型,上传者作为asset安装,那么你可以得到它:

 user.asset.file.extension 

至于将其发送给用户,如果你调用user.asset_url ,它将为你提供上传文件的URL。 用户可以使用该链接获取文件。 或者我误解了“将文件发送给用户”的含义?