【问题标题】:CarrierWave isn't uploading imageCarrierWave 没有上传图片
【发布时间】:2018-07-01 08:30:25
【问题描述】:

我正在尝试使用carrierwave gem 将用户个人资料图片从我的应用程序上传到一个集中式应用程序。在 params 中,头像已通过但未更新。

这是我的申请日志:

Started PATCH "/api/v1/users/47" for ::1 at 2018-07-01 13:45:14 +0530
Processing by Api::V1::UsersController#update as JSON
  Parameters: {"user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x00007f7c7cda6e20 @tempfile=#<Tempfile:/tmp/RackMultipart20180701-10347-otl55f.jpg>, @original_filename="leotolstoy1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"leotolstoy1.jpg\"\r\nContent-Type: image/jpeg\r\n">, "first_name"=>"Mayuresh"}, "id"=>"47"}

并集中应用日志:

Started POST "/api/v1/users/update" for 127.0.0.1 at 2018-07-01 13:45:15 +0530
Processing by Api::V1::UsersController#update as */*
  Parameters: {"first_name"=>"Mayuresh", "last_name"=>nil, "avatar"=>{"tempfile"=>"#<File:0x00007f7c7c6ba468>", "original_filename"=>"leotolstoy1.jpg", "content_type"=>"image/jpeg", "headers"=>"Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"leotolstoy1.jpg\"\r\nContent-Type: image/jpeg\r\n"}, "user"=>{"first_name"=>"Mayuresh", "last_name"=>nil, "avatar"=>{"tempfile"=>"#<File:0x00007f7c7c6ba468>", "original_filename"=>"leotolstoy1.jpg", "content_type"=>"image/jpeg", "headers"=>"Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"leotolstoy1.jpg\"\r\nContent-Type: image/jpeg\r\n"}}}
  Application Load (0.4ms)  SELECT  "oauth_applications".* FROM "oauth_applications" WHERE "oauth_applications"."secret" = $1 ORDER BY "oauth_applications"."id" ASC LIMIT $2  [["secret", "<secret_token>"], ["LIMIT", 1]]
  AccessToken Load (0.4ms)  SELECT  "oauth_access_tokens".* FROM "oauth_access_tokens" WHERE "oauth_access_tokens"."active" = $1 AND "oauth_access_tokens"."token" = $2 ORDER BY "oauth_access_tokens"."id" ASC LIMIT $3  [["active", true], ["token", "<access_token>"], ["LIMIT", 1]]
  User Load (0.6ms)  SELECT  "users".* FROM "users" WHERE "users"."id" = $1 ORDER BY "users"."id" ASC LIMIT $2  [["id", 1], ["LIMIT", 1]]
(1.8ms)  BEGIN
  User Exists (1.5ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 AND ("users"."id" != $2) AND "users"."type" = $3 LIMIT $4  [["email", "<email>"], ["id", 1], ["type", "User"], ["LIMIT", 1]]
  SQL (0.4ms)  UPDATE "users" SET "first_name" = $1, "updated_at" = $2 WHERE "users"."id" = $3  [["first_name", "Mayuresh"], ["updated_at", 2018-07-01 08:15:15 UTC], ["id", 1]]
   (12.7ms)  COMMIT

如您所见,仅更新了名字而不是头像。

中心化应用中的头像上传器:

class AvatarUploader < CarrierWave::Uploader::Base
  # Include RMagick or MiniMagick support:
  # include CarrierWave::RMagick
  # include CarrierWave::MiniMagick

  # Choose what kind of storage to use for this uploader:
  storage :file
  # storage :fog

  # Override the directory where uploaded files will be stored.
  # This is a sensible default for uploaders that are meant to be mounted:
  def store_dir
    # "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    'tmp/upload/'
  end

  # Provide a default URL as a default if there hasn't been a file uploaded:
  # def default_url(*args)
  #   # For Rails 3.1+ asset pipeline compatibility:
  #   # ActionController::Base.helpers.asset_path("fallback/" + [version_name, "default.png"].compact.join('_'))
  #
  #   "/images/fallback/" + [version_name, "default.png"].compact.join('_')
  # end

  # Process files as they are uploaded:
  # process scale: [200, 300]
  #
  # def scale(width, height)
  #   # do something
  # end

  # Create different versions of your uploaded files:
  # version :thumb do
  #   process resize_to_fit: [50, 50]
  # end

  # Add a white list of extensions which are allowed to be uploaded.
  # For images you might use something like this:
  def extension_whitelist
    %w(jpg jpeg png)
  end

  def content_type_whitelist
    /image\//
  end

  # Override the filename of the uploaded files:
  # Avoid using model.id or version_name here, see uploader/store.rb for details.
  # def filename
  #   "something.jpg" if original_filename
  # end
end

即使我试图从集中式应用程序的 Rails 控制台更新,它也没有得到更新。

2.3.4 :049 > user.update avatar: 'https://static1.squarespace.com/static/586af0132994caa37cca0067/5876e27f9f74561d8c5ce4df/5876e296c534a514869fe56e/1484187414154/Tree+On+The+Bank.jpg'
   (0.7ms)  BEGIN
  User Exists (0.8ms)  SELECT  1 AS one FROM "users" WHERE "users"."email" = $1 AND ("users"."id" != $2) AND "users"."type" = $3 LIMIT $4  [["email", "<email>"], ["id", 1], ["type", "User"], ["LIMIT", 1]]
  SQL (0.4ms)  UPDATE "users" SET "updated_at" = $1, "avatar" = $2 WHERE "users"."id" = $3  [["updated_at", 2018-07-01 07:39:16 UTC], ["avatar", "abc.jpg"], ["id", 1]]
   (13.4ms)  COMMIT
 => true

无法弄清楚这里出了什么问题。

【问题讨论】:

    标签: ruby-on-rails-5 carrierwave image-uploading


    【解决方案1】:

    问题是传递给集中式应用程序的文件对象不是原始文件对象,因为根据代码,我将参数转换为 json,然后通过法拉第建立连接以连接到集中式应用程序。

    我所做的是建立一个单独的法拉第连接来上传文件并传递参数,因为它在有效负载中如下所示:

    conn = Faraday.new url: url do |f|
      f.request :multipart
      f.request :url_encoded
      f.adapter Faraday.default_adapter
    end
    
    payload = { avatar: Faraday::UploadIO.new(update_params[:avatar], update_params[:avatar].content_type, update_params[:avatar].original_filename) }
    
    conn.put '/api/v1/users/update', payload
    

    现在我的应用程序参数:

    Started PATCH "/api/v1/users/47" for ::1 at 2018-07-01 13:45:14 +0530
    Processing by Api::V1::UsersController#update as JSON
      Parameters: {"user"=>{"avatar"=>#<ActionDispatch::Http::UploadedFile:0x00007f7c7cda6e20 @tempfile=#<Tempfile:/tmp/RackMultipart20180701-10347-otl55f.jpg>, @original_filename="leotolstoy1.jpg", @content_type="image/jpeg", @headers="Content-Disposition: form-data; name=\"user[avatar]\"; filename=\"leotolstoy1.jpg\"\r\nContent-Type: image/jpeg\r\n">, "first_name"=>"Mayuresh"}, "id"=>"47"}
    

    及集中式应用参数:

    Started PUT "/api/v1/users/update" for 127.0.0.1 at 2018-07-05 02:12:04 +0530
    Processing by Api::V1::UsersController#update as */*
      Parameters: {"avatar"=>#<ActionDispatch::Http::UploadedFile:0x0000000775e1d8 @tempfile=#<Tempfile:/tmp/RackMultipart20180705-23054-1cbcry3.png>, @original_filename="nio.png", @content_type="image/png", @headers="Content-Disposition: form-data; name=\"avatar\"; filename=\"leotolstoy1.jpg\"\r\nContent-Length: 272286\r\nContent-Type: image/png\r\nContent-Transfer-Encoding: binary\r\n">}
    

    您可以看到 avatar 参数是实际的 ActionDispatch 对象,这就是图片正确上传的原因。

    对于远程 url,我得到了解决方案 here。如下所示:

    2.3.4 :049 > user.update remote_avatar_url: 'https://static1.squarespace.com/static/586af0132994caa37cca0067/5876e27f9f74561d8c5ce4df/5876e296c534a514869fe56e/1484187414154/Tree+On+The+Bank.jpg'
    

    【讨论】:

      猜你喜欢
      • 2013-01-31
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-15
      相关资源
      最近更新 更多