My code, i have Jquery:
<div class="form-row">
<input class="form-group col-md-6" id="fileinput" type="file" accept="image/gif, image/jpeg, image/png" onchange="readURL(this);" />
</div>
<div class="form-group">
<label for="content">Message</label>
<textarea id="content" name="content" rows="10" class="form-control" placeholder="Écrivez un message ..."></textarea>
</div>
<script>
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$("#fileinput").attr("src", e.target.result);
$("#content").val(e.target.result);
};
reader.readAsDataURL(input.files[0]);
}
}
</script>
I want to add the base64 img in a bbcode editor (textarea is #content). Actually is work, but i want to have this:
[img]data:image/png;base64,iVBORw0KGgoAAAAN...[/img]
Thank you.
Assuming you've successfully included jQuery as your script suggests, and your HTML looks a bit like this (simplified):
<html>
<body>
<input type="file" id="file"/>
<img id="content"/>
</body>
</html>
Then a script like this will do what you want:
$(function() {
function readURL() {
// Grabbing the DOM object, so that the code follows your general pattern
input = $(this)[0];
if (input.files && input.files.length) {
var reader = new FileReader();
reader.onload = function () {
// The attribute you want to change on an img tag is "src" not "val"
$("#content").attr("src", reader.result);
}
reader.readAsDataURL(input.files[0]);
}
}
// Bind the readURL function to the change event on our file input
$("#file").change(readURL);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.