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

File control selection picture example tutorial

Jul 26, 2017 pm 04:56 PM
file picture

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

<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:

1

2

<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:

1

2

3

4

5

6

7

8

9

10

11

12

<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>

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!

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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting? Mar 22, 2024 am 08:06 AM

How to solve the problem of automatically saving pictures when publishing on Xiaohongshu? Where is the automatically saved image when posting?

How to post pictures in TikTok comments? Where is the entrance to the pictures in the comment area? How to post pictures in TikTok comments? Where is the entrance to the pictures in the comment area? Mar 21, 2024 pm 09:12 PM

How to post pictures in TikTok comments? Where is the entrance to the pictures in the comment area?

6 Ways to Make Pictures Sharper on iPhone 6 Ways to Make Pictures Sharper on iPhone Mar 04, 2024 pm 06:25 PM

6 Ways to Make Pictures Sharper on iPhone

How to make ppt pictures appear one by one How to make ppt pictures appear one by one Mar 25, 2024 pm 04:00 PM

How to make ppt pictures appear one by one

Hongmeng native application random poetry Hongmeng native application random poetry Feb 19, 2024 pm 01:36 PM

Hongmeng native application random poetry

How to convert pdf documents into jpg images with Foxit PDF Reader - How to convert pdf documents into jpg images with Foxit PDF Reader How to convert pdf documents into jpg images with Foxit PDF Reader - How to convert pdf documents into jpg images with Foxit PDF Reader Mar 04, 2024 pm 05:49 PM

How to convert pdf documents into jpg images with Foxit PDF Reader - How to convert pdf documents into jpg images with Foxit PDF Reader

How to use JavaScript to implement the drag and zoom function of images? How to use JavaScript to implement the drag and zoom function of images? Oct 27, 2023 am 09:39 AM

How to use JavaScript to implement the drag and zoom function of images?

How to use HTML, CSS and jQuery to implement advanced functions of image merging and display How to use HTML, CSS and jQuery to implement advanced functions of image merging and display Oct 27, 2023 pm 04:36 PM

How to use HTML, CSS and jQuery to implement advanced functions of image merging and display

See all articles