Note that there are some explanatory texts on larger screens.

plurals
  1. PORESTful file uploads with CarrierWave
    primarykey
    data
    text
    <p>I'm trying to build an API backend for file uploads. I want to be able to upload files with a POST request that has a Base64-encoded string of the file. The server should decode the string, and save the file using CarrierWave. Here's what I have so far:</p> <p>photo.rb:</p> <pre><code>class Photo include Mongoid::Document include Mongoid::Timestamps mount_uploader :image_file, ImageUploader end </code></pre> <p>image_uploader.rb:</p> <pre><code>class ImageUploader &lt; CarrierWave::Uploader::Base storage :file def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end end </code></pre> <p>Rails console: (summary)</p> <pre><code>ruby-1.8.7-p334 :001 &gt; img = File.open("../image.png") {|i| i.read} =&gt; "\377���JFIF\000\001\002\001\000H\000H\000\000\377�Photoshop 3.0\0008BIM\003... ruby-1.8.7-p334 :003 &gt; encoded_img = Base64.encode64 img =&gt; 3af8A\nmLpplt5U8q+a7G2... ruby-1.8.7-p334 :005 &gt; p = Photo.new =&gt; #&lt;Photo _id: 4e21b9a31d41c817b9000001, created_at: nil, updated_at: nil, _type: nil, user_id: nil, image_file_filename: nil&gt; ruby-1.8.7-p334 :006 &gt; p.user_id = 1 =&gt; 1 ruby-1.8.7-p334 :007 &gt; p.image_file = Base64.decode64 encoded_img \255��=\254\200�7u\226���\230�-zh�wT\253%����\036ʉs\232Is�M\215��˿6\247\256\177... ruby-1.8.7-p334 :008 &gt; p.save =&gt; true ruby-1.8.7-p334 :009 &gt; p.image_file.url =&gt; nil </code></pre> <p><a href="http://pastebin.com/KByEJTQA" rel="noreferrer">full</a></p> <p>The problem appears to be related to the process of converting a Base64-decoded string to a file. CarrierWave seems to expect a File object, and instead I'm giving it a String. So how do I convert that String to a File object. I'd like this conversion not to save anything to the file system, simply create the object and let CarrierWave do the rest.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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