Implement the Image Upload local preview function based on jquery and jquery Image Upload

Source: Internet
Author: User

Implement the Image Upload local preview function based on jquery and jquery Image Upload

When we upload a file to the server every time, we can preview the file. It seems reasonable. If the network speed is slow or the image is faulty, this not only wastes customers' time but also server resources. Next we will introduce how to use js to upload images for local preview. I hope this method will be helpful to you.
I. Principles

There are two steps:

When the input of the uploaded image is triggered and the local image is selected, the URL of the object (Object URL) of the image to be uploaded is obtained );

Assign the object URL to the src attribute of the previously written img tag to display the image.

Here, we need to understand the File object, Blob Object and window. URL. createObjectURL () method in Javascript.

1. File object

The File object can be used to obtain information about a File and read the content of the File. generally, a File object is a FileList object returned after you select a File on an input element. It can also be a DataTransfer object generated by drag-and-drop operations.

The following describes how to obtain the FileList object:

<Script type = "text/javascript" src = "jquery. js "> </script> <input id =" upload "type =" file ">  <script type =" text/javascript "> $ ('# upload '). change (function () {// obtain the first alert (document. getelementbyid ('upload '). files [0]) ;}); </script>

2. Blob Object

A Blob Object is a class file object that contains read-only raw data. data in Blob objects is not necessarily in the native form of JavaScript. the File interface is based on Blob, inherits the Blob function, and supports the extension of local files on the user's computer.

The object URL we want to obtain is actually obtained from the Blob object, because the File interface inherits Blob. The following describes how to convert a Blob object to a URL:

<script type="text/javascript">var f = document.getelementbyid('upload').files[0];var src = window.URL.createObjectURL(f);document.getElementById('preview').src = src;</script>

A complete instance

<Html> 

Ii. Compatibility

  • • The above method applies to chrome
  • • For IE browser, you can directly use the input value to replace src.
  • • You can use the getAsDataURL () method of the File object to obtain the URL. Now, this method has been abolished, and similar methods include getAsText () and getAsBinary;

The above is all the content of this article, hoping to help you learn.

Articles you may be interested in:
  • Implementation Code of jquery batch upload of images
  • Jquery-based code for Image Upload and image size verification and image Preview
  • Jquery Image Upload proportional preview plug-in Set
  • Jquery's ajaxSubmit () asynchronously uploads images and saves form data DEMO code
  • Jquery supports local preview of images uploaded by browsers
  • How to Use the jquery upload plugin fineuploader to upload files (jquery Image Upload plugin)
  • Use jQuery to verify the format and size of the uploaded Image
  • PHP + jQuery + Ajax for multi-Image Upload
  • JQuery enables local preview and upload of images

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.