【发布时间】:2015-11-29 14:54:24
【问题描述】:
我正在将现有应用程序从 ASP.NET 转换为 Rails,并且在上传 Base64 图像时遇到了困难。我正在发送为 image 参数编码的图像 base64。我正在使用 CarrierWave 处理图像并将其存储在 S3 上。我已经尝试了所有我找不到的 stackoverflow 文章。
型号:
class Image < ActiveRecord::Base
self.table_name = "Images"
self.primary_key = "ImageId"
has_many :image_comments, foreign_key: :ImageId
belongs_to :location, foreign_key: :LocationId
belongs_to :company, foreign_key: :CompanyId
accepts_nested_attributes_for :image_comments
alias_attribute :id, :ImageId
alias_attribute :active, :IsActive
alias_attribute :filename, :Filename
alias_attribute :date_uploaded, :DateUploaded
alias_attribute :user_id, :UploadedById
alias_attribute :notes, :Notes
alias_attribute :location_id, :LocationId
alias_attribute :lat, :Lat
alias_attribute :lon, :Lon
alias_attribute :company_id, :CompanyId
alias_attribute :anonymous, :IsAnnonymousLocation
alias_attribute :update_ticks, :UpdateTicks
alias_attribute :url_large, :FullSizeUrl
alias_attribute :url_medium, :WebSizeUrl
alias_attribute :url_small, :MobileSizeUrl
alias_attribute :image, :FullSizeUrl
alias_attribute :date_received, :DateReceived
mount_uploader :url_large, OriginalImageUploader
end
控制器
def create
@image = @location.images.build(image_params)
if @image.save
render json: @image, serializer: V1::ImageSerializer
else
api_error(status: 422, errors: @image.errors)
end
end
上传者
# encoding: utf-8
require 'image_io'
class OriginalImageUploader < CarrierWave::Uploader::Base
include CarrierWave::MiniMagick
storage :fog
before :cache, :convert_base64
def convert_base64(file)
filename = "test.jpg"
content_type = "image/jpeg"
decoded = Base64.decode64(file.read)
file.tempfile.close!
decoded = ImageIO.new(decoded)
decoded.original_filename = filename
decoded.content_type = content_type
file.send :file=, decoded
end
# process :set_content_type
# process :resize_to_fit => [1334, 1334]
# process :quality => 75
# def extension_white_list
# %w(jpg jpeg png)
# end
end
图像IO
class ImageIO < StringIO
attr_accessor :original_filename
attr_accessor :content_type
end
【问题讨论】:
标签: ruby-on-rails image carrierwave