最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

jquery ajax實(shí)現(xiàn)文件上傳功能實(shí)例代碼

 更新時(shí)間:2018年04月18日 11:57:25   作者:清風(fēng)白水  
這篇文章主要介紹了jquery ajax實(shí)現(xiàn)文件拖拽上傳功能的實(shí)例代碼,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下

下面看下ajax實(shí)現(xiàn)文件上傳

    沒(méi)有使用插件

一、單文件上傳

<!DOCTYPE html> 
<html> 
<head lang="en"> 
 <meta charset="UTF-8"> 
 <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script> 
 <title></title> 
</head> 
<body> 
<form id="uploadForm" enctype="multipart/form-data"> 
 文件:<input id="file" type="file" name="file"/> 
</form> 
<button id="upload">上傳文件</button> 
</body> 
<script type="text/javascript"> 
 $(function () { 
 $("#upload").click(function () { 
  var formData = new FormData($('#uploadForm')[0]); 
  $.ajax({ 
  type: 'post', 
  url: "http://192.168.1.101:8080/springbootdemo/file/upload", 
  data: formData, 
  cache: false, 
  processData: false, 
  contentType: false, 
  }).success(function (data) { 
  alert(data); 
  }).error(function () { 
  alert("上傳失敗"); 
  }); 
 }); 
 }); 
</script> 
</html> 

二、多文件上傳

<!DOCTYPE html> 
<html> 
<head lang="en"> 
 <meta charset="UTF-8"> 
 <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script> 
 <title></title> 
</head> 
<body> 
<form id="uploadForm" enctype="multipart/form-data"> 
 文件:<input type="file" name="file" multiple="multiple"/><br> 
</form> 
<button id="upload">上傳文件</button> 
</body> 
<script type="text/javascript"> 
 $(function () { 
 $("#upload").click(function () { 
  var formData = new FormData($('#uploadForm')[0]); 
  $.ajax({ 
  type: 'post', 
  url: "http://192.168.1.101:8080/springbootdemo/file/uploadFiles", 
  data: formData, 
  cache: false, 
  processData: false, 
  contentType: false, 
  }).success(function (data) { 
  alert(data); 
  }).error(function () { 
  alert("上傳失敗"); 
  }); 
 }); 
 }); 
</script> 
</html> 

這個(gè)是多選上傳,關(guān)鍵是multiple="multiple"這個(gè)屬性,另外使用的接口也是多文件上傳的接口。

當(dāng)然也可以使用單文件上傳的模式,多次選擇就可以了,只不過(guò)接口也是iyaoshiyong多文件上傳的接口。

<!DOCTYPE html> 
<html> 
<head lang="en"> 
 <meta charset="UTF-8"> 
 <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script> 
 <title></title> 
</head> 
<body> 
<form id="uploadForm" enctype="multipart/form-data"> 
 文件:<input type="file" name="file"/><br> 
 文件:<input type="file" name="file"/><br> 
 文件:<input type="file" name="file"/><br> 
</form> 
<button id="upload">上傳文件</button> 
</body> 
<script type="text/javascript"> 
 $(function () { 
 $("#upload").click(function () { 
  var formData = new FormData($('#uploadForm')[0]); 
  $.ajax({ 
  type: 'post', 
  url: "http://192.168.1.101:8080/springbootdemo/file/uploadFiles", 
  data: formData, 
  cache: false, 
  processData: false, 
  contentType: false, 
  }).success(function (data) { 
  alert(data); 
  }).error(function () { 
  alert("上傳失敗"); 
  }); 
 }); 
 }); 
</script> 
</html> 

測(cè)試都通過(guò)了?。。?br />

下面通過(guò)一段實(shí)例代碼給大家介紹ajax拖拽上傳功能的實(shí)現(xiàn),具體代碼如下;

AJAX拖拽上傳功能,實(shí)現(xiàn)代碼如下所示:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <meta http-equiv="X-UA-Compatible" content="ie=edge">
 <title>Document</title>
 <style>
 .box {
 width: 300px;
 height: 300px;
 border: 1px solid #000;
 text-align: center;
 line-height: 300px;
 font-size: 40px;
 }
 </style>
</head>
<body>
 <div class="box">+</div>
 <script>
 var box = document.querySelector('.box');
 box.ondragover = function (e) {
 e.preventDefault();
 }
 box.ondrop = function (e) {
 console.log(e.dataTransfer)
 e.preventDefault();
 var xhr = new XMLHttpRequest();
 xhr.onreadystatechange = function () {
 if (xhr.readyState == 4 && xhr.status == 200) {
  console.log(xhr.responseText)
 }
 }
 xhr.open('POST', './server.php', true);
 var formdata = new FormData();
 formdata.append('pic', e.dataTransfer.files[0]);
 formdata.append('name', 'luyao');
 xhr.send(formdata);
 }
 </script>
</body>
</html>
//server.php
<?php
 $rand = rand(1,1000).'.jpg';
 move_uploaded_file($_FILES['pic']['tmp_name'], './uploads/'.$rand);
 echo '/uploads/'.$rand;

總結(jié)

以上所述是小編給大家介紹的jquery ajax實(shí)現(xiàn)文件上傳功能實(shí)例代碼,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論

上栗县| 灵川县| 汾西县| 壤塘县| 东辽县| 文登市| 泸溪县| 罗城| 秭归县| 沈阳市| 湖南省| 新郑市| 蕲春县| 辉县市| 开封市| 金湖县| 新建县| 芦溪县| 沽源县| 长白| 车致| 涞水县| 澄城县| 海原县| 沙田区| 三都| 楚雄市| 遂川县| 固始县| 高唐县| 淮南市| 清河县| 金堂县| 桐城市| 丰原市| 民和| 囊谦县| 南郑县| 太仓市| 浦县| 津南区|