使用 Paperclip 和 Multipart 请求将文件上传到 Rails JSON API 服务器
我想将文件从 Android 客户端上传到 Rails JSON API 服务器.
I want to upload a file from an Android client to a Rails JSON API server.
我正在从 Android 客户端发送一个 Multipart/form 请求,如下所示:
I'm sending a Multipart/form request from the Android client which looks like that:
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--
在 rails 控制器中,我正在使用以下代码创建新帖子:
in the rails controller i'm creating the new post with this code:
@parsed_json = JSON(params[:POSTDATA])
@post = @current_user.posts.new(@parsed_json["post"])
如何让 Paperclip 保存多部分表单中的附件?
how do I make Paperclip save the attachment from the multipart form ?
我可以这样做:
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.
纯 json 方法是不传递 content-type multipart-form,而是将文件作为 json 中的 base64 编码字符串传递.
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.
我想通了,感谢这篇文章:http://www.rqna.net/qna/xyxun-paperclip-throws-nohandlererror-with-base64-photo.html
I figured this out thanks this post: http://www.rqna.net/qna/xyxun-paperclip-throws-nohandlererror-with-base64-photo.html
这是一个 json 示例:
Here's an example of the 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