葡萄和回形针上传文件 [英] uploading file with grape and paperclip

查看:231
本文介绍了葡萄和回形针上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个REST API,试图上传用户的图片与

I'm working on a REST API, trying to upload a picture of user with:


  • 葡萄微架构

  • 回形针宝石,但它不工作,表现出这个错误

  • 导轨版本是3.2.8

  • grape micro-framework
  • paperclip gem but it's not working, showing this error
  • rails version is 3.2.8

没有处理发现#&LT; HASHIE ::醪文件名=user.png头=内容处置:表格数据;名称= \\图片\\;文件名= \\ user.png \\\\ r \\ nContent类型:图像/ PNG \\ r \\ nNAME =图片 tempfile=#<File:/var/folders/7g/b_rgx2c909vf8dpk2v00r7r80000gn/T/RackMultipart20121228-52105-43ered>类型=图像/ PNG&GT;

我试着用控制器的测试回形针和它的工作,但是当我尝试通过葡萄API上传它不工作我的帖子标题为的multipart / form-data的

I tried testing paperclip with a controller and it worked but when I try to upload via grape api its not working my post header is multipart/form-data

我的code为上传此

 user = User.find(20) 
 user.picture = params[:picture] 
 user.save! 

所以,如果它是不可能通过上传文件的葡萄有通过REST API上传文件的另一种方式?

So if it is not possible to upload files via grape is there any alternative way to upload files via REST api?

推荐答案

@艾哈迈德·谢里夫解决方案的作品,但你失去original_filename(和扩展名),并能提供preprocessors和验证器发生问题的。您可以使用 ActionDispatch :: HTTP :: UploadedFile的是这样的:

@ahmad-sherif solution works but you loose original_filename (and extension) and that can provide probems with preprocessors and validators. You can use ActionDispatch::Http::UploadedFile like this:

  desc "Update image"
  params do
    requires :id, :type => String, :desc => "ID."
    requires :image, :type => Rack::Multipart::UploadedFile, :desc => "Image file."
  end
  post :image do
    new_file = ActionDispatch::Http::UploadedFile.new(params[:image])
    object = SomeObject.find(params[:id])
    object.image = new_file
    object.save
  end

这篇关于葡萄和回形针上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆