HTML5 drag-and-drop local files to display on a Web page

Source: Internet
Author: User


An object provided in the HTML5 standard for file input and output (I/O)
File: represents a Document object
FileList: represents a list of files, class array objects
FileReader for extracting content from files
FileWrite is used to write a class tolerance to a file.

These objects need to be accessed and retrieved through E.datatransfer in the H5 drag event

The complete results are as follows:

The Data URL gives us a clever way to "embed" the image into HTML. Unlike the traditional way of using img tags to refer pictures on a server to a page, in the Data URL protocol, the picture is converted to a base64 encoded string and stored in the URL, labeled Mime-type.

<! DOCTYPE html>

Document.ondragover = function (e) {e.preventdefault ();

The default behavior is only blocked in OnDragOver to trigger OnDrop instead of OnDragLeave
}; Document.ondrop = function (e) {e.preventdefault ();

Prevent Document.ondrop from default behavior * * Open a dragged picture in a new window
};
/* Drag the source object-----A picture of the client */* Drag the target object-----Div#container If the picture is released above this element, it needs to be displayed in the */
Container.ondragover = function (e) {e.preventdefault (); }; Container.ondrop = function (e) {console.log (e.datatransfer);
Chrome is displayed incorrectly here
var list = E.datatransfer.files; var f = list[0];//Console.log (f);       var reader = new FileReader (); Reader Object
Reads the contents of the specified file as a "data url"
Reader.readasdataurl (f);
The onload event is triggered when the client file reads complete reader.onload = function () {
Console.log (Reader.result); var img = new Image (); IMG.SRC = Reader.result; Container.appendchild (IMG);

} };                              </script></body>

  

The pits encountered are as follows: Chrome displays errors



HTML5 drag-and-drop local files to display on a Web page

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.