Carrierwave how to get the file extension

前端 未结 2 1221
忘了有多久
忘了有多久 2021-02-05 12:53

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. T

2条回答
  •  眼角桃花
    2021-02-05 13:39

    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
    

提交回复
热议问题