Html5 implements drag-and-drop File Upload

Source: Internet
Author: User

Html file


[Html]
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml" lang = "zh-CN">
<Head>
<Title> HTML5 drag-and-drop upload </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Meta name = "description" content = ""/>
<Meta name = "keywords" content = ""/>
<Style type = "text/css">
 
# Dropzone {
Width: 300px;
Height: 300px;
Border: 2px dashed gray;
}
 
# Dropzone. over {
Border: 2px dashed orange;
}
 
</Style>
</Head>
<Body>
<H1> drag/drop upload <Div id = "dropzone"> </div>
</Body>
 
<Script type = "text/javascript">
 
// Sends data through ajax.
Function up (fd ){
Var xhr = new XMLHttpRequest ();
Xhr. open ('post', 'upfile. php', true); // asynchronous transmission
 
// Xhr. upload this is a new html5 api that stores information during the upload process.
Xhr. upload. onprogress = function (ev ){
Var percent = 0;
If (ev. lengthComputable ){
Percent = 100 * ev. loaded/ev. total;
// Document. getElementById ('progress'). innerHTML = percent;
Document. getElementById ('bar'). style. width = percent + '% ';
}
}
 
Xhr. send (fd );
}
 
 
Var dz = document. getElementById ('dropzone ');
Dz. ondragover = function (ev ){
This. className = 'over ';
Return false;
}
 
Dz. ondragleave = function (){
This. className = '';
}
 
Dz. ondrop = function (ev ){
// Console. log (ev. dataTransfer. files [0]);
 
Var fd = new FormData ();
Fd. append ('pic ', ev. dataTransfer. files [0]);
 
Up (fd );
 
Return false; // intercept normal drag and drop Behaviors
}
 
</Script>
</Html>

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Strict // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml" lang = "zh-CN">
<Head>
<Title> HTML5 drag-and-drop upload </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Meta name = "description" content = ""/>
<Meta name = "keywords" content = ""/>
<Style type = "text/css">

# Dropzone {
Width: 300px;
Height: 300px;
Border: 2px dashed gray;
}

# Dropzone. over {
Border: 2px dashed orange;
}

</Style>
</Head>
<Body>
<H1> drag/drop upload <Div id = "dropzone"> </div>
</Body>

<Script type = "text/javascript">

// Sends data through ajax.
Function up (fd ){
Var xhr = new XMLHttpRequest ();
Xhr. open ('post', 'upfile. php', true); // asynchronous transmission

// Xhr. upload this is a new html5 api that stores information during the upload process.
Xhr. upload. onprogress = function (ev ){
Var percent = 0;
If (ev. lengthComputable ){
Percent = 100 * ev. loaded/ev. total;
// Document. getElementById ('progress'). innerHTML = percent;
Document. getElementById ('bar'). style. width = percent + '% ';
}
}

Xhr. send (fd );
}


Var dz = document. getElementById ('dropzone ');
Dz. ondragover = function (ev ){
This. className = 'over ';
Return false;
}

Dz. ondragleave = function (){
This. className = '';
}

Dz. ondrop = function (ev ){
// Console. log (ev. dataTransfer. files [0]);

Var fd = new FormData ();
Fd. append ('pic ', ev. dataTransfer. files [0]);

Up (fd );

Return false; // intercept normal drag and drop Behaviors
}

</Script>
</Html>
Upfile. php


[Php]
Echo move_uploaded_file ($ _ FILES ['pic '] ['tmp _ name'],'. /upload /'. $ _ FILES ['pic '] ['name'])? 'OK': 'fail ';

Echo move_uploaded_file ($ _ FILES ['pic '] ['tmp _ name'],'. /upload /'. $ _ FILES ['pic '] ['name'])? 'OK': 'fail ';

 
 

 

Related Article

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.