我有一个API,我在Base64中从Angular Client发送图像,我的方法创建的下一个代码:
if params[:profile][:picture]
picture_params = params[:profile][:picture]
#Create new Temporal File
temp_file = Tempfile.new("file_to_upload")
temp_file.binmode
temp_file.write(Base64.decode64(picture_params["body"]))
#Create a new uploaded file
uploaded_file = ActionDispatch::Http::UploadedFile.new(
:tempfile => temp_file,
:original_filename => picture_params[:name])
#After replace picture with the new uploaded file
params[:profile][:picture] = uploaded_file
Rails.logger.info "#{params[:profile]}"
end
@profile = @current_user.build_profile(profile_params)
if @profile.save
render "api/v1/profiles/show"
else
errors_array!(@profile.errors.full_messages, :unprocessable_entity)
end
当我打印我的参数后,信息是零
"picture"=>#<ActionDispatch::Http::UploadedFile:0x007f872daac5b0 @tempfile=#<Tempfile:/var/folders/nx/30024wp17ggfgw542lyzs4zw0000gn/T/file_to_upload20160225-14952-by947r>, @original_filename=nil, @content_type=nil, @headers=nil>}
在尝试创建配置文件之后,我有下一个错误,我认为这是针对上一个问题
NoMethodError (undefined method `gsub' for nil:NilClass):
app/controllers/api/v1/profiles_controller.rb:39:in `create'
我在Rails 4.2.5.1中使用paperclip gem上传文件
答案 0 :(得分:1)
感谢您的观看,但有一个解决方案,我改变了方法:
def set_selfies(selfie_params)
picture_params = selfie_params
encoded_picture = picture_params[:body]
content_type = picture_params[:content_type]
image = Paperclip.io_adapters.for("#{encoded_picture}")
image.original_filename = picture_params[:name]
image
end
I Read this Blog: 这一切都对我有用,谢谢:D