I'm currently working on a ruby on rails project. In the project I have a form with a input file type (An image) and I need to convert the image to base64 (The project connects to an external api, so the image needs to be in base64)
So far I have tried to do this Base64.encode64(target_params[:image].read)
but I get an empty string as result.
Just solve it using this code:
file =
target_params[:image].tempfile.open.read.force_encoding(Encoding::UTF_8)
Base64.encode64(file)
Related
Am keeping base64 encoded string
context.put("imagebase64str", base64str);
and using the following to get the image in template
Am printing in my console and generated template works if we save as html file and opens.. Image getting displayed perfectly..
When it invoked for lamda mail then image not getting loaded.
Kindly let me know what to be done for the fix.
Thanks
i read this post convert image to base64 with flutter but this is about converting image file to base64. How to convert online url to base64.
Right now i can think of one solution that is to store image in path and get that file path and convert it. thats how this post showing.
is there any other sort way to convert online image uri to base64?
Well to convert the image you need the image data otherwise there's nothing to convert. So the step for the most effecient would be.
Perform get request to the image url
Read all the bytes from the response body
Convert to base64
Save the base 64 string locally or use how you please.
You don't have to save it. Just keep in mind if the user closes the app or it stops at any point during this process you'll have to start it from the beginning because you're not saving the image to disk.
I'm using RoR and I have a base 64 encoded image image_base64 = "data:image/jpeg;base64,/9j/4AAQSkZ...".
An API asks me to upload it as JPG file in a multipart/form-data parameter - e.g curl -X POST -F "images_file=#my_image.jpg"
I know how to turn the base 64 image into a file that would be saved somewhere (like in this other SO question), but I don't really want to be saving and deleting files, just create one on the fly. How can I do that?
I am using the Carrierwave gem to upload attachments to my model. I added elasticsearch with the mapper attachments plugin to allow for full text search of the attachments.
Carrierwave and elasticsearch work fine, but in order to get the full text search working I need to encode the attachment as base64.
I have followed this tutorial (http://rny.io/rails/elasticsearch/2013/08/05/full-text-search-for-attachments-with-rails-and-elasticsearch.html) but I assume there has been some changes to either Rails or Carrierwave as I can't get it to work. Specifically, when I am trying to encode the attachment as base64, I get the following Type error:
no implicit conversion of CarrierWave::SanitizedFile into String
The error is in the following line of the model:
File.open(Base64.encode64(File.read(document.file)))
If I replace the path with a url to an actual file it works fine.
I have searched SO and the only relevant answer I can find gives me the same error: Carrierwave encode file to base64 as process
I am a complete rails newbie and hopefully this is something that's obvious to everyone except me, but we're all beginners at first, right?
Thanks!
CarrierWave's read method returns the content of the file. So assuming Document is your model and file is your uploader attribute, this should work:
Base64.encode64(document.file.read)
I am trying to convert a base64 string into a video file for use in a gem.
How do you take the base string and reformat it to what it was before it came through http.
Use Base64.decode and write directly the results to a file.
http://ruby-doc.org/stdlib-2.1.5/libdoc/base64/rdoc/Base64.html