Image Convert to Base64

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')->pat‌​h()));

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



Leave a reply



Submit