How to show upload image preview using jquery before form submit in a div
By : Topher Bear
Date : March 29 2020, 07:55 AM
|
Show an image preview before upload
By : JPenney
Date : March 29 2020, 07:55 AM
wish help you to fix your issue HTML5 comes with File API spec, which allows you to create applications that let the user interact with files locally; That means you can load files and render them in the browser without actually having to upload the files. Part of the File API is the FileReader interface which lets web applications asynchronously read the contents of files . Here's a quick example that makes use of the FileReader class to read an image as DataURL and renders a thumbnail by setting the src attribute of an image tag to a data URL: code :
<input type="file" id="files" />
<img id="image" />
document.getElementById("files").onchange = function () {
var reader = new FileReader();
reader.onload = function (e) {
// get loaded data and render thumbnail.
document.getElementById("image").src = e.target.result;
};
// read the image file as a data URL.
reader.readAsDataURL(this.files[0]);
};
function handleFileSelect(evt) {
var files = evt.target.files;
// Loop through the FileList and render image files as thumbnails.
for (var i = 0, f; f = files[i]; i++) {
// Only process image files.
if (!f.type.match('image.*')) {
continue;
}
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function(theFile) {
return function(e) {
// Render thumbnail.
var span = document.createElement('span');
span.innerHTML =
[
'<img style="height: 75px; border: 1px solid #000; margin: 5px" src="',
e.target.result,
'" title="', escape(theFile.name),
'"/>'
].join('');
document.getElementById('list').insertBefore(span, null);
};
})(f);
// Read in the image file as a data URL.
reader.readAsDataURL(f);
}
}
document.getElementById('files').addEventListener('change', handleFileSelect, false);
<input type="file" id="files" multiple />
<output id="list"></output>
|
Show preview image, upload on submit form
By : George Ellams
Date : March 29 2020, 07:55 AM
|
How to show image preview before Upload?
By : cguercio
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , You can use URL.createObjectURL() to create a Blob URL of uploaded image. Adjusted html to as appended parent element instead of element. code :
$(document).on('click', '.close', function() {
$(this).siblings("img").toggle();
})
document.getElementById("uploadBtn").onchange = function() {
document.getElementById("uploadFile").value = this.value;
};
document.getElementById('uploadBtn').onchange = uploadOnChange;
function uploadOnChange() {
var filename = this.value;
var lastIndex = filename.lastIndexOf("\\");
if (lastIndex >= 0) {
filename = filename.substring(lastIndex + 1);
}
var files = $('#uploadBtn')[0].files;
for (var i = 0; i < files.length; i++) {
(function(i) {
$("#upload_prev").append('<div><br><div class="col-md-10"><span class="uploadFiles">' + '<a href="">' + files[i].name + '</a>' + '</span></div><div class="col-md-2"><p class="close" style="font-size: 13pt;">削除</p><br></div></div>');
$("#upload_prev a:contains(" + files[i].name + ")")
.on("click", function(e) {
e.preventDefault();
var close = $(this).closest("div")
.next("div")
.find(".close");
if (!$(this).closest("div")
.next("div").find("img").length)
close
.after(
$("<img>", {
src: URL.createObjectURL(files[i])
})
)
else
close.siblings("img").toggle()
})
})(i);
}
document.getElementById('filename').value = filename;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="uploadBtn" type="file" class="upload" multiple="multiple" accept="image/*" name="browsefile" style="display: none !important;" />
<input type="button" value="ファイル追加" onclick="document.getElementById('uploadBtn').click();" style="float: right;" />
<input id="filename" type="hidden" />
<br>
<div id="upload_prev"></div>
<div style="clear:both;"></div>
|
Show preview of Image and upload file with AngularJs
By : Mitchell
Date : March 29 2020, 07:55 AM
it helps some times You have to convert the file data that is read to Base 64 string format. Then you can use that base 64 data to show the image.
|