How to preview an image before it is uploaded?
function readURL(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
$('#blah').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$("#imgInp").change(function(){
readURL(this);
});
Copy Code
HTML code:
<form id="form1" runat="server">
<input type='file' id="imgInp" />
<img id="blah" src="#" alt="my image" />
</form>
Copy Code
There are two ways. The most efficient way would be to use URL.createObjectURL()
on the File from your <input> . Pass this URL to img.src to the browser to load the provided image.
<input type="file" accept="image/*" onchange="loadFile(event)">
<img id="output"/>
<script>
var loadFile = function(event) {
var output = document.getElementById('output');
output.src = URL.createObjectURL(event.target.files[0]);
};
</script>
Copy Code
Also use FileReader.readAsDataURL() to parse the file from your <input>. It will create a string in memory containing a base 64 representation of the image .
<input type="file" accept="image/*" onchange="loadFile(event)">
<img id="output"/>
<script>
var loadFile = function(event) {
var reader = new FileReader();
reader.onload = function(){
var output = document.getElementById('output');
output.src = reader.result;
};
reader.readAsDataURL(event.target.files[0]);
};
</script>
Copy Code
Using an object URL viewing a large image is more efficient than compared to data URL.
<img id="para" alt="your image" width="100" height="100" />
<input type="file"
onchange="document.getElementById('para').src = window.URL.createObjectURL(this.files[0])">
Copy Code
In HTML have two preview elements:
<img id="preview"
src=""
alt=""
style="display:none; max-width: 160px; max-height: 120px; border: none;"/>
<div id="preview_ie"></div>
Copy Code
In CSS we specify the following IE specific thing:
#preview_ie {
FILTER: progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)
}
Copy Code
In HTML we include the standard and the IE-specific Javascripts:
<script type="text/javascript">
{% include "pic_preview.js" %}
</script>
<!--[if gte IE 7]>
<script type="text/javascript">
{% include "pic_preview_ie.js" %}
</script>
Copy Code
pic_preview.js
is the Javascript. $('#blah')
replace by $('#preview')
and add the $('#preview').show()
:
function readURL (imgFile) {
var newPreview = document.getElementById('preview_ie');
newPreview.filters.item('DXImageTransform.Microsoft.AlphaImageLoader').src = imgFile.value;
newPreview.style.width = '160px';
newPreview.style.height = '120px';
}
Copy Code
Using this code to display "No Preview Available" image, if it is not an image:
function readURL(input) {
var url = input.value;
var ext = url.substring(url.lastIndexOf('.') + 1).toLowerCase();
if (input.files && input.files[0]&& (ext == "gif" || ext == "png" || ext == "jpeg" || ext == "jpg")) {
var reader = new FileReader();
reader.onload = function (e) {
$('.imagepreview').attr('src', e.target.result);
}
reader.readAsDataURL(input.files[0]);
}else{
$('.imagepreview').attr('src', '/assets/no_preview.png');
}
}
Copy Code