Using HTML file APIs, Firefox, chrome, and other browsers can drag and drop files from the operating system and upload them to the server.
Compared with HTML forms that have been used for more than 10 years, this is a revolutionary progress. Although many developers are still waiting for the Internet Explorer to lag behind, the drag-and-drop feature of Gmail mail has brought great convenience to some users, the CMS (Content Management System) that requires a large number of files to be uploaded will also benefit from this.
Let's take a look at how Firefox uses the drag/drop upload function:
First, provide a region for file placement.
HtmlCode
<Div name = "image" id = "Dropbox" style = "Min-width: 300px; Min-Height: 100px; Border: 3px dashed silver;"> </div>
Then listen to events such as dragenter, dragleave, and drop during the drag process.
JS Code
Document. addeventlistener ("dragenter", function (e) {Dropbox. style. bordercolor = 'Gray ';}, false); document. addeventlistener ("dragleave", function (e) {Dropbox. style. bordercolor = 'sil';}, false); Dropbox. addeventlistener ("dragenter", function (e) {Dropbox. style. bordercolor = 'Gray '; Dropbox. style. backgroundcolor = 'white';}, false); Dropbox. addeventlistener ("dragleave", function (e) {Dropbox. style. backgroundcolor = 'transparent';}, false); Dropbox. addeventlistener ("dragenter", function (e) {e. stoppropagation (); E. preventdefault () ;}, false); Dropbox. addeventlistener ("dragover", function (e) {e. stoppropagation (); E. preventdefault () ;}, false); Dropbox. addeventlistener ("Drop", function (e) {e. stoppropagation (); E. preventdefault (); handlefiles (E. datatransfer. files); Submit. disabled = false ;}, false );
Handlefiles () is used in the drop event to process all files in sequence.
JS Code
Handlefiles = function (Files) {for (VAR I = 0; I <files. length; I ++) {var file = files [I] ;}}
You can directly read the content of an image file to display a preview.
JS Code
If (! File. type. match (/image */) {continue;} var IMG = document. createelement ("IMG"); IMG. classlist. add ("OBJ"); IMG. file = file; preview. appendchild (IMG); var reader = new filereader (); reader. onload = (function (aimg) {return function (e) {aimg. src = e.tar get. result ;};}) (IMG); reader. readasdataurl (File );
The next step is the core function: Ajax upload. Create an xhr request
JS Code
VaR xhr = new XMLHttpRequest (); xhr. Open ('post', '/file/upload', true );
Listen for upload progress and completion events
JS Code
Xhr. upload. addeventlistener ("progress", function (e) {If (E. lengthcomputable) {var percentage = math. round (E. loaded * 100)/E. total); IMG. style. opacity = 1-percentage/100.0; }}, false); xhr. upload. addeventlistener ("LOAD", function (e) {}, false );
Finally, the data is uploaded in the multipart/form-data format.
JS Code
xhr. setRequestHeader ("Content-Type", "multipart/form-data, boundary =" + boundary); // simulate a file mime POST request. xhr. setRequestHeader ("Content-Length", filesize); var body = ''; body + =" -- "+ boundary +" \ r \ n "; body + = "content-Disposition: Form-data; name = \" "+ Dropbox. getattribute ('name') + "\"; filename = \ "" + filename + "\" \ r \ n "; body + =" Content-Type: "+ filetype +" \ r \ n "; body + = filedata +" \ r \ n "; body + = "--" + boundary + "-- \ r \ n"; xhr. sendasbinary (body);