Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I was actually having a terrible time with this question yesterday to do something very similar. In fact, I wrote the question: <a href="https://stackoverflow.com/questions/9854916/base64-upload-from-android-java-to-ror-carrierwave">Base64 upload from Android/Java to RoR Carrierwave</a></p> <p>What it came down to was creating that uploaded image object in the controller and then injecting it back into the params. </p> <p>For this specific example, we are taking a base64 file (which I assume you have, as JSON doesn't support embeded files) and saving it as a temp file in the system then we are creating that UploadedFile object and finally reinjecting it into the params. </p> <p>What my json/params looks like:</p> <pre><code>picture {:user_id =&gt; "1", :folder_id =&gt; 1, etc., :picture_path {:file =&gt; "base64 awesomeness", :original_filename =&gt; "my file name", :filename =&gt; "my file name"}} </code></pre> <p>Here is what my controller looks like now:</p> <pre><code> # POST /pictures # POST /pictures.json def create #check if file is within picture_path if params[:picture][:picture_path]["file"] picture_path_params = params[:picture][:picture_path] #create a new tempfile named fileupload tempfile = Tempfile.new("fileupload") tempfile.binmode #get the file and decode it with base64 then write it to the tempfile tempfile.write(Base64.decode64(picture_path_params["file"])) #create a new uploaded file uploaded_file = ActionDispatch::Http::UploadedFile.new(:tempfile =&gt; tempfile, :filename =&gt; picture_path_params["filename"], :original_filename =&gt; picture_path_params["original_filename"]) #replace picture_path with the new uploaded file params[:picture][:picture_path] = uploaded_file end @picture = Picture.new(params[:picture]) respond_to do |format| if @picture.save format.html { redirect_to @picture, notice: 'Picture was successfully created.' } format.json { render json: @picture, status: :created, location: @picture } else format.html { render action: "new" } format.json { render json: @picture.errors, status: :unprocessable_entity } end end end </code></pre> <p>The only thing left to do at this point is to delete the tempfile, which I believe can be done with <code>tempfile.delete</code></p> <p>I hope this helps with your question! I spent all day looking for a solution yesterday, and everything I have seen is a dead end. This, however, works on my test cases. </p>
    singulars
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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