Use jQuery to preview thumbnails before uploading images

Source: Internet
Author: User

Use jQuery to preview thumbnails before uploading images

JQuery code

01 $("#uploadImage").on("change"function(){
02     // Get a reference to the fileList
03     var files = !!this.files ? this.files : [];
04   
05     // If no files were selected, or no FileReader support, return
06     if (!files.length || !window.FileReader) return;
07   
08     // Only proceed if the selected file is an image
09     if (/^image/.test( files[0].type)){
10   
11         // Create a new instance of the FileReader
12         var reader = new FileReader();
13   
14         // Read the local file as a DataURL
15         reader.readAsDataURL(files[0]);
16   
17         // When loaded, set image data as background of div
18         reader.onloadend = function(){
19    
20        $("#uploadPreview").css("background-image""url("+this.result+")");
21          
22         }
23   
24     }
25   
26 });

The following is an HTML code that contains a div that displays thumbnails and a file input field.

HTML code

1 <div id="uploadPreview"></div>
2 <input id="uploadImage" type="file" name="photoimage" class="fimg1" onchange="PreviewImage();" />

Set CSSwa for our thumbnails.

CSS code

1 <strong>#uploadPreview {
2     width168px;
3     height168px;                         
4     background-positioncenter center;
5     background-size: cover;
6     border4px solid #fff;
7     -webkit-box-shadow: 0 0 1px 1px rgba(000, .3);
8     display: inline-block;</strong>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.