简体   繁体   中英

NameError: uninitialized constant MIME

This line of rubypress code:

:type => MIME::Types.type_for(FILENAME).first.to_s,

is barfing with the following error:

NameError: uninitialized constant MIME

The whole block is:

FILENAME='myFile.png'
wp.uploadFile(:data => {
    :name => FILENAME,
    :type => MIME::Types.type_for(FILENAME).first.to_s,
    :bits => XMLRPC::Base64.new(IO.read(FILENAME))
    })

Any suggestions what the problem is?

require 'mime/types'添加到文件顶部。

extension = File.extname(file).split(".")[1] Mime::Type.lookup_by_extension(extension)

A little late to the party.

After a recent Ruby upgrade, I've found that MIME::TYPES could not be initialized. A more modern solution is to use marcel , which is already included as a dependency with the recent rails version.

Marcel::MimeType.for(file)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM