js多文件上传
一、HTML
選擇文件的時候可以選擇多個文件,這個需要我們在input file 里面加入一個屬性multiple="multiple" 這樣就可以框選文件了
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"/> <title>多文件上傳</title> </head><body> <div style="text-align:center;margin:100px"><input type="file" id="file" name="file" multiple="multiple"><button onclick="xhr2()">多文件上傳</button> </div> </body> </html>二、JS
下面使用到的知識參考自:
http://www.cnblogs.com/snowinmay/archive/2013/07/17/3195072.html
http://www.jb51.net/article/89998.htm
https://my.oschina.net/u/1866405/blog/335987
//多文件上傳函數 function xhr2(){var xhr = new XMLHttpRequest();//第一步//定義表單變量var file = document.getElementById('file').files;//console.log(file.length);//新建一個FormData對象var formData = new FormData(); //++++++++++//追加文件數據for(i=0;i<file.length;i++){ formData.append("file["+i+"]", file[i]); //++++++++++ } //formData.append("file", file[0]); //++++++++++//post方式xhr.open('POST', '2.php'); //第二步驟//發送請求xhr.send(formData); //第三步驟//ajax返回xhr.onreadystatechange = function(){ //第四步if ( xhr.readyState == 4 && xhr.status == 200 ) {console.log( xhr.responseText );//打印數據到控制臺 }};//設置超時時間xhr.timeout = 100000;xhr.ontimeout = function(event){alert('請求超時!');} }三、PHP
這里就是簡單的接收文件和移動文件
if($_POST){print_r($_FILES["file"]); for($i=0;$i<count($_FILES["file"]['name']);$i++){$name=$_FILES["file"]["name"][$i];move_uploaded_file($_FILES["file"]["tmp_name"][$i],iconv("UTF-8","gb2312",$name));} }?
轉載于:https://www.cnblogs.com/phpyangbo/p/6148327.html
總結
- 上一篇: 【java】定时器
- 下一篇: 关于子网划分—为什么全0全1子网号不能使