ajax上传(qqfile)和回形针的编码错误

发布于 2025-01-03 00:55:42 字数 1524 浏览 0 评论 0原文

我正在尝试使用 Rails 3.1.3 和回形针进行 ajax 上传。

我找到了解决我的问题的方法 Rails 3 获取原始发布数据并将其写入 tmp 文件,但使用此方法时,我收到“编码未定义转换”错误“\xFF”从 ASCII-8BIT 到 UTF-8

错误发生在行 @user.photo = @user.photo = QqFile.parse(params[:qqfile], request)

我没有编辑上一个答案中提供的代码,但我'将其包含在这里,这样您就不必来回切换。

gem 列表回形针,返回 2.5.2, 2.4.5, 2.3.8

我的控制器

 def create


    @user = User.new(params[:user])

    @user.photo = QqFile.parse(params[:qqfile], request)
    if @user.save
        return render :json => @user
    else
        return render :json => @user.errors
    end
  end

qq_file.rb

# encoding: utf-8
require 'digest/sha1'
require 'mime/types'

# Usage (paperclip example)
# @asset.data = QqFile.new(params[:qqfile], request)
class QqFile < ::Tempfile

  def initialize(filename, request, tmpdir = Dir::tmpdir)
    @original_filename  = filename
    @request = request

    super Digest::SHA1.hexdigest(filename), tmpdir
    fetch
  end

  def self.parse(*args)
    return args.first unless args.first.is_a?(String)
    new(*args)
  end

  def fetch
    self.write @request.raw_post
    self.rewind
    self
  end

  def original_filename
    @original_filename
  end

  def content_type
    types = MIME::Types.type_for(@request.content_type)
      types.empty? ? @request.content_type : types.first.to_s
  end
end

I'm trying to get an ajax upload working with rails 3.1.3 and paperclip.

I found this solution to my problem Rails 3 get raw post data and write it to tmp file, but using this, I get an 'encoding undefined conversion error "\xFF" from ASCII-8BIT to UTF-8.

The error occurs at the line @user.photo = @user.photo = QqFile.parse(params[:qqfile], request)

I have not edited the code supplied in the previous answer, but I'll include it here so you don't have to switch back and forth.

the gem list paperclip, returns 2.5.2, 2.4.5, 2.3.8

my controller

 def create


    @user = User.new(params[:user])

    @user.photo = QqFile.parse(params[:qqfile], request)
    if @user.save
        return render :json => @user
    else
        return render :json => @user.errors
    end
  end

qq_file.rb

# encoding: utf-8
require 'digest/sha1'
require 'mime/types'

# Usage (paperclip example)
# @asset.data = QqFile.new(params[:qqfile], request)
class QqFile < ::Tempfile

  def initialize(filename, request, tmpdir = Dir::tmpdir)
    @original_filename  = filename
    @request = request

    super Digest::SHA1.hexdigest(filename), tmpdir
    fetch
  end

  def self.parse(*args)
    return args.first unless args.first.is_a?(String)
    new(*args)
  end

  def fetch
    self.write @request.raw_post
    self.rewind
    self
  end

  def original_filename
    @original_filename
  end

  def content_type
    types = MIME::Types.type_for(@request.content_type)
      types.empty? ? @request.content_type : types.first.to_s
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦里兽 2025-01-10 00:55:43

这是与 Ruby 1.9.2(或者我相信是 Ruby 1.9+)相关的​​编码错误。
这篇 github 帖子给出了答案
https://github.com/lassebunk/webcam_app/issues/1

您必须指定 <据我所知(我不是一个伟大的程序员),在读取上传时,code>raw_post.force_encoding("UTF-8")。

This was an encoding error related to Ruby 1.9.2 (or I believe Ruby 1.9+).
this github post lead to the answer
https://github.com/lassebunk/webcam_app/issues/1

You must specify raw_post.force_encoding("UTF-8") when reading an upload as far as I could tell (I'm not a great programmer).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文