HTML5 file Drag

Source: Internet
Author: User
Tags bind file size sublime text

HTML5 the new file API, you can get the name, file size, type, and so on, you need to first on the DOM element to drag and drop event binding
Related APIs

To get the node first, bind the event dragged to the node to change the mouse shape

var draghere = document.queryselector ("#draghere");
Draghere.addeventlistener ("DragOver", function (e) {
E.preventdefault ();
E.stoppropagation ();
E.datatransfer.dropeffect = "Copy";
});

When the mouse is loosened, then bind the drop event and use Event.dataTransfer.files to get a list of files

Draghere.addeventlistener ("Drop", function (e) {
E.preventdefault ();
E.stoppropagation ();
var filelists = e.datatransfer.files;
}

After you get to the specified file, and then get the file name and file size, you can determine whether to continue the operation by this information

var file = Filelists[0];
var fileName = File.name;
var fileSize = File.size

If you need to get the contents of a file, you first have to determine whether the file is loaded, and then use the FileReader class's Readastext (file) method to read files
Get content from Reader's Result property

var reader = new FileReader ();
Reader.onloadend = function (e) {
if (e.target.readystate = = Filereader.done) {
Get file Contents
var filecontent = Reader.result;
}
}
Reader.readastext (file);

Small sample

Drag the file to the Web page, determine whether to read the content based on the file name and type, and then use the powerful online editor codemirror to show the results
Codemirror supports vim and Emacs, as well as example sublime Text, which also supports a number of shortcut keys.

Online sample



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.