Home  >  Article  >  Web Front-end  >  File control selection picture example tutorial

File control selection picture example tutorial

零下一度
零下一度Original
2017-07-26 16:56:041499browse

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:

        3f1c4e4b6b16bbbd69b2ee476dc4f83a$(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;

                    }                });
            }
            );
            2cacc6d41bbb37262a98f745aa00fbf0

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn