Skip to content

Instantly share code, notes, and snippets.

@dimkoug
Last active March 25, 2020 14:22
Show Gist options
  • Save dimkoug/255debd8873fca3ab30fe46d647ec3b1 to your computer and use it in GitHub Desktop.
Save dimkoug/255debd8873fca3ab30fe46d647ec3b1 to your computer and use it in GitHub Desktop.
Add multiple files upload with jquery
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
<style media="screen">
.form_img {
width:300px;
height:300px;
padding:2px;
}
</style>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script type="text/javascript">
function readURL(input) {
console.info(input.files.length);
if (input.files && input.files[0]) {
console.info(input.files[0]);
$.each(input.files, function( key, value ) {
console.info( key + ": " + value );
var reader = new FileReader();
reader.onload = function(e) {
var img = $('<img class="form_img">'); //Equivalent: $(document.createElement('img'))
img.attr('src', e.target.result);
img.appendTo('.result');
}
reader.readAsDataURL(input.files[key]); // convert to base64 string
});
}
}
$(document).ready(function(){
$("#imgInp").change(function() {
$(".result").html("");
readURL(this);
});
})
</script>
</head>
<body>
<div class="result"></div>
<form runat="server">
<input type='file' id="imgInp" multiple />
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment