How to display the FileUpload control on img at the same time when browsing and selecting pictures (without pressing upload) The following code can achieve this:
I have been looking for this for a long time. I will give you mine and you can try it again. .
Js part:
<script type="text/javascript"> function ShowImg(obj) { var AllowExt = ".jpg|.gif|.bmp|.png|"; var FileExt = obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase(); if (AllowExt != 0 && AllowExt.indexOf(FileExt + "|") == -1) //判断文件类型是否允许上传 { alert("您上传的不是图片!"); } else { var newPreview = document.getElementById("PreviewImg2"); newPreview.src = obj.value; } } </script>
Body part:
<img id="PreviewImg2" alt="" src="" width="140px" height="115px" /> <asp:FileUpload ID="FileUpload1" runat="server" onchange="ShowImg(this)" />
This method was tested by me personally and can be used. I hope it will be helpful to you! If it works, don’t forget to like it! ! !
The code is as follows:
<script>$(function() { $("#Book_Fiel").change(function() {var $file = $(this);var fileObj = $file[0];var windowURL = window.URL || window.webkitURL;var dataURL;var $img = $("#Book_Picture");if (fileObj && fileObj.files && fileObj.files[0]) { dataURL = windowURL.createObjectURL(fileObj.files[0]); $img.attr('src', dataURL); } else { dataURL = $file.val();var imgObj = document.getElementById("preview");// 两个坑:// 1、在设置filter属性时,元素必须已经存在在DOM树中,动态创建的Node,也需要在设置属性前加入到DOM中,先设置属性在加入,无效;// 2、src属性需要像下面的方式添加,上面的两种方式添加,无效;imgObj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)"; imgObj.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = dataURL; } }); } ); </script>
The above is the detailed content of File control selection picture example tutorial. For more information, please follow other related articles on the PHP Chinese website!