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

File control selection picture example tutorial

零下一度
Release: 2017-07-26 16:56:04
Original
1568 people have browsed it

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>
Copy after login

Body part:

<img id="PreviewImg2" alt="" src="" width="140px" height="115px" />
 <asp:FileUpload ID="FileUpload1" runat="server" onchange="ShowImg(this)" />
Copy after login

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(&#39;src&#39;, 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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template