使用 CarrierWave 做一个上传图片的接口,要求客户端图片经过 base64 编码。
if params[:picture]["file"]
picture_path_params = params[:picture]
tempfile = Tempfile.new("fileupload")
tempfile.binmode
tempfile.write(Base64.decode64(picture_path_params["file"]))
#create a new uploaded file
uploaded_file = ActionDispatch::Http::UploadedFile.new(:tempfile => tempfile, :filename => picture_path_params["filename"], :original_filename => picture_path_params["original_filename"])
params[:picture] = uploaded_file
end
uploader = ChatBase64Uploader.new
uploader.store!(params[:picture])
我做了一个 java 的 demo,上传没问题。
但是 ios 那边传过来的图片解码后总提示不是
Failed to manipulate with MiniMagick, maybe it is not an image? Original Error: Command ("identify -quiet -ping /var/folders/8q/wwnwcmt93lvcynt9_5yjhqrr0000gn/T/mini_magick20140516-77009-1o5zyba.jpg_s") failed: {:status_code=>1, :output=>"identify: IHDR: CRC error `/var/folders/8q/wwnwcmt93lvcynt9_5yjhqrr0000gn/T/mini_magick20140516-77009-1o5zyba.jpg_s' @ error/png.c/MagickPNGErrorHandler/1806.\nidentify: corrupt image `/var/folders/8q/wwnwcmt93lvcynt9_5yjhqrr0000gn/T/mini_magick20140516-77009-1o5zyba.jpg_s' @ error/png.c/ReadPNGImage/4077.\n"}