i create a form where i upload image into database where i create icon input to select the image i want to convert that icon into that picture which i select after slecting the picture.
<label >
<input asp-for="imge1" name="imge1" type="file" />
<i ></i>
CSS
input[type="file"] {
display: none;
}
.custom-file-upload {
border: 1px solid #ccc;
display: inline-block;
padding: 6px 12px;
cursor: pointer;
height: 80px;
width: 100px;
display: flex;
align-items: center;
justify-content: center;
font-size: 30px;
color: var(--primary-color);
}
CodePudding user response:
little bit change and add some javascrpt and jquery in it.
firslty add this
<div id="selectedBanner"></div>
<label >
<input asp-for="imge1" name="imge1" type="file" id="img">
<i ></i>
then add this javascript and jquery
<script src="https://code.jquery.com/jquery-3.6.0.min.js"
integrity="sha256-/xUj 3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4="
crossorigin="anonymous"></script>
<script>
var selDiv = "";
var storedFiles = [];
$(document).ready(function () {
$("#img").on("change", handleFileSelect);
selDiv = $("#selectedBanner");
});
function handleFileSelect(e) {
var files = e.target.files;
var filesArr = Array.prototype.slice.call(files);
filesArr.forEach(function (f) {
if (!f.type.match("image.*")) {
return;
}
storedFiles.push(f);
var reader = new FileReader();
reader.onload = function (e) {
var html =
'<img src="'
e.target.result
"\" data-file='"
f.name
"alt='Category Image' height='50px' width='50px'>";
selDiv.html(html);
};
reader.readAsDataURL(f);
});
}
</script>
CodePudding user response:
Using readAsDataURL you can get file path and set to img tag.
jQuery(document).ready(function() {
var readURL = function(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function(e) {
$('.input-img').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("input[type='file']").on('change', function() {
readURL(this);
});
});
.custom-input-file {
position: relative;
cursor:pointer;
}
.custom-input-file .input-file-wrapper {
height: 150px;
width: 150px;
border-radius: 10px;
position: relative;
}
.custom-input-file .input-file-wrapper .input-img {
height: 100%;
-o-object-fit: cover;
object-fit: cover;
border: 1px solid #ced4da;
border-radius: 10px;
width: 100%;;
}
.custom-input-file .input-file-wrapper input[type=file] {
position: absolute;
left: 0;
width: 100%;
height: 100%;
padding: 0;
opacity: 0;
top: 0;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div >
<div >
<div >
<img src="https://storage.googleapis.com/proudcity/mebanenc/uploads/2021/03/placeholder-image.png " >
<input type="file" >
</div>
</div>
</div>
CodePudding user response:
U can use readAsDataURL and get Base64 image and put it into <img>
source
const state = {
fileMaxSize: 5,
}
const myImg = document.getElementById('img');
const input = document.getElementById('icon');
const wrap = document.getElementById('wrap');
const getBase64 = (img, callback) => {
const reader = new FileReader();
reader.addEventListener('load', () => callback(reader.result));
reader.readAsDataURL(img);
}
input.onchange = () => {
const file = [...input.files][0];
if (!file) {
return
}
const isJpgOrPng = file.type === 'image/jpeg' || file.type === 'image/png';
const isLt2M = file.size / 1024 / 1024 < state.fileMaxSize;
if (!isJpgOrPng || !isLt2M) {
if (!isJpgOrPng) {
alert('You can only upload JPG/PNG file!')
}
if (!isLt2M) {
alert(`Image must smaller than ${state.fileMaxSize}MB!`)
}
return false
}
getBase64(file, imageUrl => {
let Base64Img = imageUrl
console.log('File Object ->', file)
console.log('Base64 Image ->', Base64Img)
myImg.src = Base64Img
myImg.style.display = 'block'
wrap.style.display = 'none'
}
);
}
.hidden {
position: absolute;
width: 0;
height: 0;
overflow: hidden;
}
.image-label {
cursor: pointer;
width: 100px;
height: 100px;
display: flex;
align-items: center;
justify-content: center;
text-align: center;
box-sizing: border-box;
background-color: #fafafa;
overflow: hidden;
border:1px dashed #d9d9d9;
}
.image-label img {
display: none;
width: 100%;
height: auto;
}
<input type="file" id="icon"/>
<label for="icon">
<img src="" id="img" />
<div id="wrap">
<div>Select</div>
</div>
</label>