Base64 upload from Android/Java to RoR Carrierwave

前端 未结 1 1124
春和景丽
春和景丽 2020-12-13 22:41

I added the solution from use base64 image with Carrierwave in an effort to upload an image from a java class. This is now what my FileUploader class looks like -- and I bel

相关标签:
1条回答
  • 2020-12-13 23:12

    Finally solved the problem! I hope this answer helps out others who are trying to solve this problem as there is no good resource for it. This was surprising as I figured others would have wanted to do the same. My original changes to the Carrierwave initialize file appear to have been a dead end.

    What it came down to was creating that uploaded image object in the controller and then injecting it back into the params.

    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.

    What my json/params looks like:

    picture {:user_id => "1", :folder_id => 1, etc., :picture_path {:file => "base64 awesomeness", :original_filename => "my file name", :filename => "my file name"}}
    

    Here is what my controller looks like now:

    40        # POST /pictures
    41    # POST /pictures.json
    42    def create
    43  
    44      #check if file is within picture_path
    45      if params[:picture][:picture_path]["file"]
    46           picture_path_params = params[:picture][:picture_path]
    47           #create a new tempfile named fileupload
    48           tempfile = Tempfile.new("fileupload")
    49           tempfile.binmode
    50           #get the file and decode it with base64 then write it to the tempfile
    51           tempfile.write(Base64.decode64(picture_path_params["file"]))
    52     
    53           #create a new uploaded file
    54           uploaded_file = ActionDispatch::Http::UploadedFile.new(:tempfile => tempfile, :filename => picture_path_params["filename"], :original_filename => picture_path_params["original_filename"]) 
    55     
    56           #replace picture_path with the new uploaded file
    57           params[:picture][:picture_path] =  uploaded_file
    58     
    59      end
    60  
    61      @picture = Picture.new(params[:picture])
    62  
    63      respond_to do |format|
    64        if @picture.save
    65          format.html { redirect_to @picture, notice: 'Picture was successfully created.' }
    66          format.json { render json: @picture, status: :created, location: @picture }
    67        else
    68          format.html { render action: "new" }
    69          format.json { render json: @picture.errors, status: :unprocessable_entity }
    70        end
    71      end
    72    end
    

    The only thing left to do at this point is to delete the tempfile, which I believe can be done with tempfile.delete

    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.

    0 讨论(0)
提交回复
热议问题