简体   繁体   English

使用Paperclip和Multipart请求将文件上载到Rails JSON API服务器

[英]uploading a file to Rails JSON API server with Paperclip and Multipart request

I want to upload a file from an Android client to a Rails JSON API server. 我想将文件从Android客户端上传到Rails JSON API服务器。

I'm sending a Multipart/form request from the Android client which looks like that: 我正在从Android客户端发送一个Multipart / form请求,如下所示:

Content-Type: multipart/form-data; boundary=d99ArGa2SaAsrXaGL_AdkNlmGn2wuflo5
Host: 10.0.2.2:3000
Connection: Keep-Alive
User-Agent: Apache-HttpClient/UNAVAILABLE (java 1.4)

--d99ArGa2SaAsrXaGL_AdkNlmGn2wuflo5
Content-Disposition: form-data; name="POSTDATA"
Content-Type: application/json; charset=UTF-8
Content-Transfer-Encoding: 8bit

{"tags":["test"],"location_id":1,"post":{"content":"test"}}
--d99ArGa2SaAsrXaGL_AdkNlmGn2wuflo5
Content-Disposition: form-data; name="IMAGEDATA"; filename="testimage.jpg"
Content-Type: image/jpeg
Content-Transfer-Encoding: binary

<BINARY DATA?
--d99ArGa2SaAsrXaGL_AdkNlmGn2wuflo5--

in the rails controller i'm creating the new post with this code: 在rails控制器中我用这段代码创建新帖子:

@parsed_json = JSON(params[:POSTDATA])
@post = @current_user.posts.new(@parsed_json["post"]) 

how do I make Paperclip save the attachment from the multipart form ? 如何使Paperclip从多部分表单中保存附件?

I can do it with something like this: 我可以这样做:

if params.has_key?(:IMAGEDATA)
    photo = params[:IMAGEDATA]
    photo.rewind

    @filename = "/tmp/tempfile"
    File.open(@filename, "wb") do |file|
      file.write(photo.read)
    end

    @post.photo = File.open(@filename)
  end

but it doesn't look like the best solution, also, the filename that is being passed in ther multipart request is not used. 但它看起来不是最佳解决方案,也不会使用多部分请求中传递的文件名。

The pure json way to do this is to not pass content-type multipart-form and pass the file as a base64 encoded string in the json. 纯json方法是不传递content-type multipart-form并将文件作为base64编码的字符串传递给json。

I figured this out thanks this post: http://www.rqna.net/qna/xyxun-paperclip-throws-nohandlererror-with-base64-photo.html 我想通了这篇帖子: http//www.rqna.net/qna/xyxun-paperclip-throws-nohandlererror-with-base64-photo.html

Here's an example of the json: 这是json的一个例子:

"{\"account\":{\"first_name\":\"John\",\"last_name\":\"Smith\",\"email\":\"john@test.com\",\"password\":\"testtest\",\"avatar\":{\"data\":\"INSERT BASE64 ENCODED STRING OF FILE HERE\",\"filename\":\"avatar.jpg\",\"content_type\":\"image/jpg\"}}}"

Then in the controller process the incoming avatar like this before saving the model. 然后在控制器进程中输入这样的头像,然后保存模型。

def process_avatar
  if params[:account] && params[:account][:avatar]
    data = StringIO.new(Base64.decode64(params[:account][:avatar][:data]))
    data.class.class_eval { attr_accessor :original_filename, :content_type }
    data.original_filename = params[:account][:avatar][:filename]
    data.content_type = params[:account][:avatar][:content_type]
    params[:account][:avatar] = data
  end
end

So, I'm guessing your Post model looks something like this: 所以,我猜你的Post模型看起来像这样:

class Post < ActiveRecord::Base
  has_attached_file :photo, :styles => { ... }
  ...
end

So you should be able to do something as simple as this: 所以你应该能够做到这么简单:

@post.photo = params[:IMAGEDATA] if params[:IMAGEDATA].present?
@post.save if @post.valid?

And it should save the photo. 它应该保存照片。

If you need to do something more complicated, try re-arranging the form data into the data that the format Paperclip expects. 如果您需要执行更复杂的操作,请尝试将表单数据重新排列为Paperclip期望格式的数据。 And if you need to dig deeper, take a look inside Paperclip's Paperclip::Attachment class . 如果你需要深入挖掘, 请查看Paperclip的Paperclip::Attachment

Stack Overflow Cross-Reference 堆栈溢出交叉引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM