multiple多图选择,借助FormData通过ajax实现上传

前端之家收集整理的这篇文章主要介绍了multiple多图选择,借助FormData通过ajax实现上传前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<!--测试页面-->

  1. <html>
  2. <head>
  3. <script>
  4. var files;
  5. var url = "/upimage/3g_xhr.PHP";
  6. function getPath(fileQuery){
  7. var i;
  8. files =fileQuery.files;
  9. var imgContainer=document.getElementById("imgPanel");
  10. for(i=0;i<files.length;i++){
  11. var reader = new FileReader();
  12. var filename=files[i].name;
  13. reader.onload =(function(filename){
  14. return function(e){
  15. var obj=document.createElement("tr");
  16. obj.innerHTML="<td><img title='"+filename
  17. +"' src='"+e.target.result
  18. +"' style='width:100;height:100;' /></td><td "
  19. +" style='vertical-align:bottom;'>"+filename+"</td>";
  20. imgContainer.appendChild(obj);
  21. };
  22. })(filename);
  23. reader.readAsDataURL(files[i]);
  24. }
  25. }
  26. function getvl(obj){
  27. getPath(obj);
  28. }
  29. function submitImg(){
  30. for(var j=0;j<files.length;j++){
  31. var xhr = new XMLHttpRequest ();
  32. xhr.onreadystatechange = function () {
  33. if (xhr.readyState == 4) {
  34. if ((xhr.status >= 200 && xhr.status < 300)
  35. || xhr.status == 304)
  36. //200:Success.304:Tell browser to read cache.
  37. {
  38. alert(xhr.responseText);
  39. }
  40. }
  41. }
  42. var boundary = "------" + new Date () .getTime ();
  43. xhr.open ("post",url,true );
  44. var formData = new FormData ();
  45. formData.append("name","nik22");
  46. formData.append ("upfile",files[j]);
  47. xhr.send (formData);
  48. }
  49. }
  50. </script>
  51. </head>
  52. <body>
  53. <form enctype="multipart/form-data" action="" method="post"
  54. id="upform">
  55. <div id="text" style="color:#f00;"></div>
  56. 选择图片<input type="file" name="upfile[]" id="upfile"
  57. multiple="multiple" onchange="getvl(this)" />
  58. <table id="imgPanel">
  59. </table>
  60. <input type="button" value="上传" />
  61. </form>
  62. </body>
  63. </html>

<!--upimage/3g_xhr.PHP-->

  1. <?PHP
  2. echo "No. files uploaded : ".count($_FILES['upfile']['name'])."<br>";
  3. $uploadDir = "images_xhr/";
  4. /**
  5. for ($i = 0; $i < count($_FILES['upfile']['name']); $i++) {
  6. echo "File names : ".$_FILES['upfile']['name'][$i]."<br>";
  7. $ext = substr(strrchr($_FILES['upfile']['name'][$i],"."),1);
  8. // generate a random new file name to avoid name conflict
  9. $fPath = md5(rand() * time()) . ".$ext";
  10. echo "File paths : ".$_FILES['upfile']['tmp_name'][$i]."<br>";
  11. $result = move_uploaded_file($_FILES['upfile']['tmp_name'][$i],$uploadDir . $fPath);
  12. if (strlen($ext) > 0){
  13. echo "Uploaded ". $fPath ." succefully. <br>";
  14. }
  15. }
  16. echo "Upload complete.<br>"*/
  17. if($_FILES['upfile']['size'] == 0)
  18. die("<script>alert('请选择您要上传图片!');history.go(-1);
  19. </script>");
  20. $imageinfo = getimagesize($_FILES['upfile']['tmp_name']);
  21. if($imageinfo[0] > 1200 || $imageinfo[1] > 800)
  22. die("<script>alert('图片大小不符合标准(长1200宽800)!');history.go(-1);</script>");
  23. if($imageinfo[2] < 1 || $imageinfo[2] > 3)
  24. die("<script>alert('图片只能是GIF,JPG,PNG格式!');history.go(-1);</script>");
  25. //$imageinfo[2] 的值得于1,表示是gif格式, 2是jpg格式,3是png
  26. if($imageinfo[2] == 1)
  27. {
  28. $imageinfo[2] = ".gif";
  29. }
  30. elseif($imageinfo[2] == 2)
  31. {
  32. $imageinfo[2] = ".jpg";
  33. }else
  34. {
  35. $imageinfo[2] = ".png";
  36. }
  37. //文件名:把时间和文件名的md5值组合,加上后缀得到文件名。
  38. $randval = rand();
  39. $imgname = date("YmdHis").substr(md5($randval),5).$imageinfo[2];
  40. copy($_FILES['upfile']['tmp_name'],$uploadDir.$imgname) or die("move img error!");
  41. echo "Upload succefully.<br>";
  42. ?>

注:1)上面的PHP文件放到文件夹upimage下,同时在PHP所在文件夹新建文件夹images_xhr,用于存

上传图片

2)如果不通过ajax上传图片,那么将html页面中的form的action设置为/upimage/3g_xhr.PHP

提交按钮的type设置为submit,同时将PHP文件中的注释的那段去掉注释,同时将它后面的代码

注释掉,就可以实现同时提交多个图片

猜你在找的Ajax相关文章