Image convert to Base64
function readFile() {
if (!this.files || !this.files[0]) return;
const FR = new FileReader();
FR.addEventListener("load", function(evt) {
document.querySelector("#img").src = evt.target.result;
document.querySelector("#b64").textContent = evt.target.result;
});
FR.readAsDataURL(this.files[0]);
}
document.querySelector("#inp").addEventListener("change", readFile);
<input id="inp" type="file">
<p id="b64"></p>
<img id="img" height="150">
Way to convert image straight from URL to base64 without saving as a file in Python
Using the requests library:
import base64
import requests
def get_as_base64(url):
return base64.b64encode(requests.get(url).content)
How can I convert an image into Base64 string using JavaScript?
You can use the HTML5 <canvas>
for it:
Create a canvas, load your image into it and then use toDataURL()
to get the Base64 representation (actually, it's a data:
URL, but it contains the Base64-encoded image).
Convert image file into base64 image in Flutter Web
Found the solution, using readAsBytes instead of readAsBytesSync :
var bytes = await widget.image.readAsBytes();
var base64img = base64Encode(bytes);
Convert image to base 64 string with Laravel
Laravel's $request->file()
doesn't return the actual file content. It returns an instance of the UploadedFile
-class.
You need to load the actual file to be able to convert it:
$image = base64_encode(file_get_contents($request->file('image')->path()));
how can i convert image uri to base64?
You can use react-native-fs if you are using react-native cli
documentation: https://github.com/itinance/react-native-fs#readfilefilepath-string-encoding-string-promisestring
import RNFS from 'react-native-fs';
//base64 res
var data = await RNFS.readFile( "file://path-to-file", 'base64').then(res => { return res });
How to convert image data response to Image base64?
axios.get('RequestURL', { responseType: 'arraybuffer' })
.then(response => {
let blob = new Blob(
[response.data],
{ type: response.headers['content-type'] }
)
let image = window.URL.createObjectURL(blob)
return image
})
axios.get('RequestURL', {responseType: 'blob'})
.then(response => {
let imageNode = document.getElementById('image');
let imgUrl = URL.createObjectURL(response.data)
imageNode.src = imgUrl
})
How to convert an Image to base64 string in java?
The problem is that you are returning the toString()
of the call to Base64.encodeBase64(bytes)
which returns a byte array. So what you get in the end is the default string representation of a byte array, which corresponds to the output you get.
Instead, you should do:
encodedfile = new String(Base64.encodeBase64(bytes), "UTF-8");
Related Topics
Dynamically Update Values of a Chartjs Chart
How to Copy JavaScript Object to New Variable Not by Reference
(Deep) Copying an Array Using Jquery
How to Round a Number in JavaScript? .Tofixed() Returns a String
What Does Arrow Function '() => {}' Mean in JavaScript
Detect iPad Users Using Jquery
When to Use the JavaScript Mime Type Application/JavaScript Instead of Text/Javascript
Google Maps API - Getting Closest Points to Zipcode
Is a JavaScript Array Index a String or an Integer
Is Reading the 'Length' Property of an Array Really That Expensive an Operation in JavaScript
What Is the List of Possible Values for Navigator.Platform as of Today
Retrieving Binary File Content Using JavaScript, Base64 Encode It and Reverse-Decode It Using Python
Google Maps API V3 - Multiple Markers on Exact Same Spot
How to Get a JavaScript Object Property Name That Starts with a Number
How to Intercept All Ajax Requests Made by Different Js Libraries