jQuery 如何使用jQuery在弹出窗口中预览输入类型=“文件”中的选定图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18457340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to preview selected image in input type="file" in popup using jQuery?
提问by Prasad
In my code, I am allowing the user to upload an image. Now I want to show this selected image as a preview in this same popup. How can I do it using jQuery?
在我的代码中,我允许用户上传图像。现在我想在同一个弹出窗口中显示这个选定的图像作为预览。我如何使用 jQuery 做到这一点?
The following is the input type I am using in my popup window.
以下是我在弹出窗口中使用的输入类型。
HTML code:
HTML代码:
<input type="file" name="uploadNewImage">
回答by Prabu Parthipan
HTML:
HTML:
<form id="form1" runat="server">
<input type='file' id="imgInp" />
<img id="blah" src="#" alt="your image" />
</form>
jQuery
jQuery
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);
});
回答by santosh singh
If your are using HTML5 then try following code snippet
如果您使用的是 HTML5,请尝试以下代码片段
<img id="uploadPreview" style="width: 100px; height: 100px;" />
<input id="uploadImage" type="file" name="myPhoto" onchange="PreviewImage();" />
<script type="text/javascript">
function PreviewImage() {
var oFReader = new FileReader();
oFReader.readAsDataURL(document.getElementById("uploadImage").files[0]);
oFReader.onload = function (oFREvent) {
document.getElementById("uploadPreview").src = oFREvent.target.result;
};
};
</script>
回答by Abdo-Host
You can use URL.createObjectURL
您可以使用 URL.createObjectURL
<script>
function img_pathUrl(input){
$('#img_url')[0].src = (window.URL ? URL : webkitURL).createObjectURL(input.files[0]);
}
</script>
<img src="" id="img_url" alt="your image" />
<input type="file" id="img_file" onChange="img_pathUrl(this);" />
回答by MD Ashik
Just check my scripts it's working well:
只需检查我的脚本它运行良好:
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
// 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 class="thumb" 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);
#list img{
width: auto;
height: 100px;
margin: 10px ;
}
回答by Prasanna Aarthi
You can use ajax upload to preview your selected file.. http://zurb.com/playground/ajax-upload
您可以使用 ajax 上传来预览您选择的文件.. http://zurb.com/playground/ajax-upload