Drag & Drop and File Reader

Source: Internet
Author: User

Reference:

http://www.html5rocks.com/zh/tutorials/file/dndfiles/

http://blog.csdn.net/rnzuozuo/article/details/25295899

Http://www.tutorialspoint.com/html5/html5_drag_drop.htm

This article is for personal reference only

FIle Reader

Method
Abort () Stop reading
Readastext (file, "Utf-8"), the 2nd parameter is the specified decoding format, often used to read some txt text, etc.
Readasbinarystring (file), 2 binary, can be used for AJAX submissions to the backend
Readasdataurl (file), as if it were Base64, can be used to display pictures locally

Event
triggered when Onabort interrupts
OnError triggered when an error occurs
The onload file is triggered when the read completes successfully
Onloadend Read completion trigger, regardless of success or failure
Onloadstart trigger at start of Read
OnProgress Read in

//display img in local//2 Way:filereader and Createobjecturldocument.getElementById ("Upload"). AddEventListener ("Change",function(e) {varInputfile =E.target; varFiles =G.toarray (inputfile.files); varFile = Files[0]; //Note:minimum need Ie10    //createobjecturl to display img on local    varURL = window. URL | |Window.webkiturl; varIMG =NewImage (); Img.onload=function(e) {window. Url.revokeobjecturl ( This. SRC); Document.body.appendChild ( This); } img.src=url.createobjecturl (file); //Note:minimum need Ie10    varReader =NewFileReader (); Reader.onload=function(e) {varIMG =NewImage (); IMG.SRC=E.target.result;    Document.body.appendChild (IMG); } reader.readasdataurl (file);},false);

Drag & Drop

Div to be able to drag, put a draggable= "true" property,

Div to be able to drop, just AddEventListener dragover for Preventdefault (), stoppropagation () then AddEventListener drop

Transfer value can only be a string type
"Event" is not the same reference at the time of transmission

Datatransfer.getdata is a cross-browser that can

document.getElementById ("Drop"). AddEventListener ("DragOver",function(e) {e.stoppropagation ();        E.preventdefault (); Log ("Over"); }, false); document.getElementById ("Drop"). AddEventListener ("DragEnter",function(e) {e.stoppropagation ();        E.preventdefault (); Log ("Enter"); }, false); document.getElementById ("Drop"). AddEventListener ("Drop",function(e) {//var files = e.datatransfer.files;//if drop a file from window. Type Use file update        //log (datatransfer.types);//if is file = "Files", Else domstringlistE.datatransfer.dropeffect = ' move ';//copy, link, and move        vardata = E.datatransfer.getdata ("Key");        E.preventdefault (); Log ("Drop!"); }, false); document.getElementById ("Drag"). AddEventListener ("DragStart",function(e) {e.datatransfer.setdata ("Key", "must string"); E.datatransfer.effectallowed= ' Copylink ';//None, copy, Copylink, Copymove, Link, linkmove, move, all and uninitialized.         varSpecifiedimage = document.getElementById ("Liushishi"); E.datatransfer.setdragimage (Specifiedimage,100, 100);//Para2,para3 is mouse position x, y in the target element        //e.datatransfer.addelement (specifiedimage);//this method is same with setdragimage, better use setdragimage, because can set X, yLog ("Drag"); }, false);

Drag & Drop and File Reader

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.