Js+php實現(xiàn)異步拖拽上傳文件
更新時間:2015年06月23日 10:44:51 投稿:hebedich
本文給大家分享的是使用php結(jié)合js實現(xiàn)異步拖拽上傳文件的代碼,及示例,有需要的小伙伴可以參考下。
異步拖拽上傳文件--小實例
upload.html
<!DOCTYPE html> <!-- To change this license header, choose License Headers in Project Properties. To change this template file, choose Tools | Templates and open the template in the editor. --> <html> <head> <title>TODO supply a title</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <style> #box{ width:150px;height: 150px;border: 1px solid red; } </style> <script type="text/javascript" src="XMLhttpReuest.js"></script> <script> window.onload = function () { var box = document.getElementById('box'); box.ondragenter = function (e) { e.preventDefault(); } box.ondragover = function (e) { e.preventDefault(); } box.ondragleave = function (e) { e.preventDefault(); } box.ondrop = function (e) { e.preventDefault(); var file = e.dataTransfer.files[0]; var formData = new FormData(); formData.append('aa', file); var xml = ajaxFunction(); xml.open("post", './upload.php', true); xml.send(formData); xml.onreadystatechange = function () { if (xml.readyState == 4 && xml.status == 200) { var flag = xml.responseText; console.log(flag); if (flag == 1) { // box.innerHTML="上傳成功"; alert('上傳成功'); } } } } } </script> </head> <body> <div id="box"> 請拖入上傳的文件 </div> </body> </html>
upload.php
復(fù)制代碼 代碼如下:
<?php
header("Content-Type:text/html;charset=UTF-8");
if(is_uploaded_file($_FILES['aa']['tmp_name'])){
move_uploaded_file($_FILES['aa']['tmp_name'], "./".iconv("UTF-8", "GBK", $_FILES['aa']['name']));
echo '1';
}
XMLhttpReuest.js
function ajaxFunction() { var xmlHttp; try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); } catch (e) { // Internet Explorer try { xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) { try { xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) { alert("您的瀏覽器不支持AJAX!"); return false; } } } return xmlHttp; }
以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。
相關(guān)文章
統(tǒng)計出現(xiàn)最多的字符次數(shù)的js代碼
一小段代碼,經(jīng)常出現(xiàn)在面試筆試題中的:統(tǒng)計一個字符串中出現(xiàn)最多的字符的次數(shù),可以是英文或者數(shù)字。2010-12-12JS實現(xiàn)獲取漢字首字母拼音、全拼音及混拼音的方法
這篇文章主要介紹了JS實現(xiàn)獲取漢字首字母拼音、全拼音及混拼音的方法,涉及針對ChinesePY.js插件的使用及事件響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2017-11-11js/jquery解析json和數(shù)組格式的方法詳解
本篇文章主要是對js/jquery解析json和數(shù)組格式的方法進(jìn)行了詳細(xì)的介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01JavaScript中windows.open()、windows.close()方法詳解
這篇文章主要介紹了JavaScript中windows.open()、windows.close()方法詳解 的相關(guān)資料,需要的朋友可以參考下2016-07-07