为什么我收到字符串编码问题"\ xE2"?从ASCII-8BIT到UTF-8? [英] Why do I get a string encoding issue "\xE2" from ASCII-8BIT to UTF-8?

查看:527
本文介绍了为什么我收到字符串编码问题"\ xE2"?从ASCII-8BIT到UTF-8?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从电子邮件中下载PDF并将内容写入文件.由于某种原因,我收到此错误:

I'm trying to download a PDF from an email and write the contents to a file. For some reason, I'm getting this error:

An Encoding::UndefinedConversionError occurred in attachments#inbound: "\xE2" from ASCII-8BIT to UTF-8 app/controllers/api/attachments_controller.rb:70:in `write'

这是我的代码:

def inbound
    if Rails.env.production? or Rails.env.staging?
      email = Postmark::Mitt.new(request.body.read)
    else
      email = Postmark::Mitt.new(File.binread "#{Rails.root}/app/temp_pdfs/email.json")
    end

    if email.attachments.count == 0
      # notify aidin that we got an inbound email with no attachments
      respond_to do |format|
        format.json { head :no_content }
      end
      return
    end
    attachment = email.attachments.first
    filename = "attachment" + (Time.now.strftime("%Y%m%d%H%M%S")+(rand * 1000000).round.to_s) + ".pdf"
    base_path = "#{Rails.root}/temp_attachments/"
    unless File.directory?(base_path)
      Dir::mkdir(base_path)
    end
    file = File.new base_path + filename, 'w+'
    file.write Base64.decode64(attachment.source['Content'].encode("UTF-16BE", :invalid=>:replace, :replace=>"?").encode("UTF-8"))
    file.close
    write_options = write_options()
    write_options[:metadata] = {:filename => attachment.file_name, :content_type => attachment.content_type, :size => attachment.size }

    obj = s3_object()
    file = File.open file.path
    obj.write(file.read, write_options)
    file.close

    FaxAttach.trigger obj.key.split('/').last

    render :nothing => true, :status => 202 and return
  end

我看了一遍,看来解决这个问题的方法是:

I read around and it looked like the way to solve this was:

file.write Base64.decode64(attachment.source['Content'].encode("UTF-16BE", :invalid=>:replace, :replace=>"?").encode("UTF-8"))

但它似乎不起作用.

推荐答案

错误消息实际上是在文件写入时引发的,而不是您在参数内部进行的编码/解码,因为Ruby试图在file.write.为防止这种情况,最快的解决方法是在打开文件时添加b标志

The error message is actually being thrown on the file write, not by your encode/decode inside the params, because Ruby is trying to apply default character encoding on file.write. To prevent this, the quickest fix is to add the b flag when you open the file

file = File.new base_path + filename, 'wb+'
file.write Base64.decode64( attachment.source['Content'] )

这就像您的代码所暗示的那样,假设传入附件是在Base64中编码的(我无法验证这一点).存储在attachment.source['Content']中的Base64编码在ASCII-8BIT和UTF-8中应为相同的字节,因此将其在调用内转换为decode64是没有意义的.

That's assuming the incoming attachment is encoded in Base64, as your code implies (I have no way to verify this). The Base64 encoding stored inside attachment.source['Content'] should be the same bytes in ASCII-8BIT and UTF-8, so there is no point converting it inside the call to decode64.

这篇关于为什么我收到字符串编码问题"\ xE2"?从ASCII-8BIT到UTF-8?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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