How to upload file via the JSON interface in Rails - ruby-on-rails

I have a web service in Rails which among all else should provide file upload functionality to the clients. The clients all use JSON to talk to the webservice. I use the Paperclip plugin for upload management.
The problem is I do not know how to upload a file via JSON. All works in the web formular, but I cannot find information on how to consctruct my JSON Request to send files to the server. Can somebody help out?
Regards,
Angel Kafazov

You don't need to use Paperclip for uploading JSON files in rails. Use the following syntax inserting the file upload field to your view. To read the JSON files in your controllers, import JSON package to read the contents.
<%= file_field 'LABEL', 'QUERY_PARAMETER_NAME' %></p>

I didn't try this yet, but this looks promising:
http://leejava.wordpress.com/2009/07/30/upload-file-from-rest-in-ruy-on-rail-with-json-format/

Related

Rails ActiveStorage how to Fileupload via REST API

How can I upload a file from an external service or app (iOS App, Go App, etc) to a Rails REST API which uses Active Storage local file storage?
All the tutorials I can find use HTML forms. I'd like to upload my files via a POST request to the Rails API. The main thing I am uncertain about is what headers and what kind of format I need to send the files in to the backend.
The solution was to just send normal form-data. I used Postman for testing which works great for file uploads as well http://getpostman.com
Base64 is useful for you, You can decode base64 then generate temp file and upload that file.

Rails s3_direct_upload without file field

My website generates a file in javascript (audio recording) and I then want it to be uploaded to Amazon S3.
I first managed to get the uploading part working by sending the generated file to my server, where it is uploaded. However I would like now to upload the file directly to S3, without going through my server.
So I started to use the s3_direct_upload gem, which works great when using a file_field. However my file is generated by the javascript and :
- The value of a file field has to be set by the user for security reasons
- I do not want the user to have to interact with the upload
I tried to play with the S3Uploader class and to directly add data, without any success for now, it seems that I do not use the correct method.
Does anyone has any idea on how to achieve S3 direct upload without a file field ?
Thanks
Never mind, I found out that the S3Uploader class used by the s3_direct_upload gem has the same methods as the jQuery-File-Upload from which it is derived.
So one can call $("#s3_uploader").fileupload('send', {files: [f]});
And the f File will be uploaded to S3 directly

Send csv file through REST in rails

I have to send .csv file to remote server through REST in rails.
I had tried post_form of Net::HTTP and made form as multipart true, by this file get transfer (seen in parameters at both side) but at server side i am not able to read it.
It gives error "No such file or directory."
Please any can tell me is there other way to transfer file.
After finding on this and tried many things i got good solution from that i can send csv file by REST web service.
here is solution...
https://github.com/jwagener/httmultiparty
Also can use REST Client for testing purpose.

Upload and parse an Excel file in a Backbone.js app with Rails backend

There is a lot of information in the title of the question !
I'm aware of Rails gems to upload files, parse Excel, etc. but I would like to know which solution will work nice in a Backbone app (that is, I don't want to reload the page during the upload). Is there a Backbone extension for that ?
The story is: the user wants to upload an Excel file, she browses her filesystem, selects the file, click "upload"... and then the Backbone app shows the content of the file (I don't need to store the file on the server).
Well, i dont think there is any backbone extension for that :) What you need is some upload extension like uploadify of http://blueimp.github.com/jQuery-File-Upload/ . You will bind the
success method to uploader and then you can do anything with the data returned from server.

Posting files to Rails XML API

I have a Rails application setup to receive file attachments using Paperclip.
Now I need to allow a .net/C# cell phone application to post files along with the XML in the same way (or some other way if necessary: they could encode the image as base64 and send - they tried that initially - including the binary data in the tag that would normally be a file field in the web application, but it did not work.
I have found nothing in the way of documentation and wondering if anybody has experience or advice.
Surprising that there is apparently no documentation for doing this anywhere to be found. I ended up stumbling across a document on the Basecamp website describing how their file attachment process works for API users and used it as a guideline.
http://developer.37signals.com/basecamp/
with help from this article about posting files:
http://www.codevil.com/index.php/2009/05/23/posting-and-getting-files-in-rubyrails/
I modified my initial setup so that, rather than passing the tag in the XML, they first post a file and receive an file ID in response.
Then they post the XML with that reference and their .
Then I use before_validation and after_save callbacks to set the file with Paperclip, and remove the tmp file after the save.

Resources