Note that there are some explanatory texts on larger screens.

plurals
  1. POuploading a file to Rails JSON API server with Paperclip and Multipart request
    text
    copied!<p>I want to upload a file from an Android client to a Rails JSON API server.</p> <p>I'm sending a Multipart/form request from the Android client which looks like that:</p> <pre><code>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 &lt;BINARY DATA? --d99ArGa2SaAsrXaGL_AdkNlmGn2wuflo5-- </code></pre> <p>in the rails controller i'm creating the new post with this code:</p> <pre><code>@parsed_json = JSON(params[:POSTDATA]) @post = @current_user.posts.new(@parsed_json["post"]) </code></pre> <p>how do I make Paperclip save the attachment from the multipart form ?</p> <p>I can do it with something like this:</p> <pre><code>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 </code></pre> <p>but it doesn't look like the best solution, also, the filename that is being passed in ther multipart request is not used.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload