0

I want to upload a video file from IOS/Android client to RAILS JSON api server.

I am trying this from a rails api only application. For image uploading i used to encode image in base64 format and pass this Base64 value as a JSON param. But if i use the same logic, it is not practical for a large file, say for 10MB or 20MB.

Any help would be much appreciated. Thanks for the help in advance.

Vijiraj T P
  • 3
  • 1
  • 4

1 Answers1

0

It's actually a valid option to upload the file encoded in Base64 for a normal JSON-API request. You can decode the String in the Rails controller roughly with the following code:

def upload_action_method
  file = StringIO.new(Base64.decode(params[:file_data])
end

The disadvantage of this approach is how you already recognized the increased filesize of roughly ~33%.
The other way would be to perform a multi-part/form-data POST request from the IOS or Android device. This way, Rails should get access for the File object in the params hash, just like a normal file upload form. You need to build a valid request for the native device manually or use a existing library. I referenced you 2 other answers showing code examples for building a multipart request:
Android: Android- POST multipart form data
IOS: Upload image with parameters in Swift

Niklas
  • 298
  • 2
  • 7
  • Thanks for your response. I tried to implement the above said method. I used postman to pass the params with 'form-data' request, but i am getting a json parse error. ' JSON::ParserError at /tasks ============================== > 822: unexpected token at ------WebKitFormBoundarya2JDVVYPSXaBioSk Content-Disposition: form-data; name="price" 200 ------WebKitFormBoundarya2JDVVYPSXaBioSk Content-Disposition: form-data; name="pic"; filename="SampleVideo_1280x720_1mb.mp4" Content-Type: video/mp4 ' What i did, I selected a video file through postman and send the request. Can you help? – Vijiraj T P Jun 03 '17 at 07:57