Note that there are some explanatory texts on larger screens.

plurals
  1. POCarrierwave + File Uploader
    text
    copied!<p>I'm trying to figure out how to properly setup <a href="https://github.com/jnicklas/carrierwave" rel="nofollow noreferrer">Carrierwave</a> to be able to handle a file being sent via <a href="https://github.com/valums/file-uploader" rel="nofollow noreferrer">File Uploader</a>.</p> <h2>File Uploader</h2> <p>I've attached <code>fileuploader.js</code> and <code>fileuploader.css</code> to my asset pipeline (rails 3.2.0) and have the following html:</p> <pre><code>&lt;div id="file-uploader"&gt; &lt;noscript&gt; &lt;p&gt;Please enable JavaScript to use file uploader.&lt;/p&gt; &lt;!-- or put a simple form for upload here --&gt; &lt;/noscript&gt; &lt;/div&gt; </code></pre> <p>And javascript:</p> <pre><code>$(document).ready(function(){ var uploader = new qq.FileUploader({ // pass the dom node (ex. $(selector)[0] for jQuery users) element: $('#file-uploader')[0], // path to server-side upload script action: '/photos', debug: true, params: { authenticity_token: "&lt;%= form_authenticity_token.to_s %&gt;" }, onComplete: function(id, fileName, responseJSON){ alert(responseJSON.toString()); } }); }); </code></pre> <p>When I select a file it kicks off the script and my application gets called. That is what I don't know how to handle.</p> <h2>Carrierwave</h2> <p>I have a <code>Photo</code> model which has <code>mount_uploader :image, ImageUploader</code>. Therefore in <code>PhotoController</code> I have:</p> <pre><code>def create io = AppSpecificStringIO.new(request.raw_post,params[:qqfile]) @photo = Photo.new(:image =&gt; io) if @photo.save respond_to do |format| format.js { render :josn =&gt; @photo.to_json(:methods =&gt; :success, :only =&gt; [:id, :image]) } end else respond_to do |format| format.js { render :josn =&gt; {:success=&gt;false} } end end end </code></pre> <p>The param only has <code>qqfile</code> which is the file name. I found out that <code>request.raw_post</code> has the data for the image. But I don't know what format it is (is it 64Bit or not). I've been trying to find any resource I could on the subject and trying out anything. One <a href="https://github.com/jnicklas/carrierwave/wiki/How-to%3A-Upload-from-a-string-in-Rails-3" rel="nofollow noreferrer">source</a> on Carrierwave's wiki suggested to use a AppSpecificStringIO class.</p> <pre><code>class AppSpecificStringIO &lt; StringIO attr_accessor :filepath def initialize(*args) super(*args[1..-1]) @filepath = args[0] end def original_filename File.basename(filepath) end end </code></pre> <p>On File Uploader's wiki there is a <a href="https://github.com/valums/file-uploader/wiki/Rails-%2B-CarrierWave" rel="nofollow noreferrer">suggestion</a> for CarrierWave, but I can't figure anything out there either. <code>CarrierwaveStringIO</code> doesn't exist as far as I can see.</p> <p>I just want to get an ajax solution to uploading images to Carrierwave. Thank you to anyone who can help me.</p> <h3>Other links for reference:</h3> <ul> <li><a href="https://stackoverflow.com/questions/7622404/use-base64-image-with-carrierwave">base64 image with Carrierwave - SO Question</a> </li> <li><a href="https://github.com/valums/file-uploader/tree/master/server" rel="nofollow noreferrer">File uploader's notes on server side info</a> - looks like I need to handle two ways to get the file.</li> </ul>
 

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