Reprint: Use the Readasdataurl method of the FileReader object to read the image file

Source: Internet
Author: User

Article reprinted from: http://blog.okbase.net/jquery2000/archive/1296.html;

The Readasdataurl method of the FileReader object encodes the read file into the data URL. The data URL is a special technique that allows you to embed data (such as a sample) in a Web page without putting it in an external file. The advantage of using the data URL is that you do not need to send an additional HTTP request to the server to obtain additional data, and the disadvantage is that the size of the Web page may become larger. It is suitable for embedding small images, it is not recommended to encode large image files into the data URL to use. Your image file cannot exceed the browser-qualified size, or the image file cannot be read.

Refer to the following example to read an image file using Readasdataurl:

<! DOCTYPE html>

The Readasdataurl method is encoded using BASE-64, the encoded data is started by the data string, followed by the MIME type, and then the Base64 string, followed by a comma is the contents of the encoded image file.

displaying image files using img

If you want the image file to be read directly on the page, you can create a tag via JavaScript, set the SRC attribute to the data URL, and add the tag to the DOM, as shown in the following example:

<! DOCTYPE html>

Read some files

Sometimes the file you want to read is too large to read in segments, or just want to read the contents of the file section, you can cut the file, depending on the browser, you can use the following methods:

Webkitslice: Applies to browsers that support the WebKit engine, such as chrome.
Mozslice: Applies to Firefox.

Both methods are passed to the beginning of the tuple index, and the end of the bit-group index, with the index starting at 0. The following program example reads a file by using the Readasbinarystring method of the FileReader object, reading only the third byte of the file to the sixth byte group:

<! DOCTYPE html>


Please note:
Different browsers have different support for HTML 5, which can be performed properly in chrome and not necessarily in other browsers.

Reprint: Use the Readasdataurl method of the FileReader object to read the image file

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.