Cannot upload photo from app to Rails backend (with paperclip)

You must Login before you can answer or comment on any questions.

Hello,

I am building an iOS app using the latest SDK (2.1.1) and I need to upload a photo from the app to the backend which is built on Rails and uses the Paperclip gem. This upload works fine with a CURL command in the terminal but not from the app itself.

I am sending along the photo but Rails does not see it.

I am using this header and I tried with a multipart too but still no upload

httpClient.setRequestHeader ("content-type", "application/json");

1 Answer

HI Daniel

I do not know anything about rails - but if it accepts files in the same way as a standard web page with a file input tag does then you need to change the header.

Setting application/json tells the receiving end to expect json, but you are sending file data. You need to use multipart/form-data instead, which is form input fields and file inputs.

httpClient.setRequestHeader("Content-type", "multipart/form-data;");
You would then include your files in the send parcel like this;
...
httpClient.setRequestHeader("Content-type", "multipart/form-data;");
...
var param = {
    firstname: 'some',
    lastname: 'one',
    file53: image.getImage() // this is a ref to the image/file data - not a filename
};
...
httpClient.send(param);
You can have more than one file send and the name file53 is anything you want. XHR will sort the rest for you.

— answered 11 months ago by Malcolm Hollingsworth
answer permalink
3 Comments
  • Problem is that I am sending along firstname and lastname too and Rails is expecting that in json-format. Is multipart/form-data; a typo?

    — commented 11 months ago by Daniel Krusenstråhle

  • Adding the multipart header did nothing. Still same problem unfortunately.

    — commented 11 months ago by Daniel Krusenstråhle

  • Hi

    multipart/form-data is not a typo. I use this exact process myself when sending data to server with ASP, ASPX and PHP.

    JSON cannot include binary information, so whilst your firstname and lastname fields will make it - there is no way it would receive file data, I suggest that it is the ruby side that you need to review.

    In the example I gave I also included two fields, firstname and lastname, this happily co-exist with the file data.

    The technique I mentioned is the exact same as if you had create a web page form like;

    <form name='profile' action='url' method='post' enctype="multipart/form-data">
    <input type="text" name="firstname" />
    <input type="text" name="lastname" />
    <input type="file" name="files" />
    <input type="submit" value="Send" />
    </form>
    The data this form passes - is the same as would be sent using the technique suggested above, so if you could have received something through this html form, then you will with my suggestion.

    According to the paperclip documentation for ruby it also creates (uses) a multi-part form in HTML. This is a direct example from the documentation found on their site.

    <%= form_for @user, :url => users_path, :html => { :multipart => true } do |form| %>
      <%= form.file_field :avatar %>
    <% end %>

    — commented 11 months ago by Malcolm Hollingsworth

Your Answer

Think you can help? Login to answer this question!