html多文件上传,可支持预览

前端开发   发布日期:2025年06月07日   浏览次数:163
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>表单提交</title>
  6. <script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
  7. </head>
  8. <body>
  9. <!--文件上传-->
  10. <form id="uploadForm" enctype="multipart/form-data">
  11. <div id="fileId" style='display: none'><!--//style='display: none'-->
  12. </div>
  13. <div id="img-con" class="panel panel-default imgdiv">
  14. </div>
  15. <p id="em">未上传文件</p>
  16. <input type="button" value="点击事件" name="点击事件" onclick="inputClieck()"><br>
  17. <input type="submit">
  18. </form>
  19. </body>
  20. <script>
  21. var inputArray = [];
  22. function inputClieck() {
  23. var newInput = document.createElement("input");
  24. newInput.type = 'file';
  25. newInput.name = "files";
  26. var idid = new Date().getTime();
  27. newInput.id = idid;
  28. $("#fileId").append(newInput);
  29. inputArray.push(idid);
  30. $("#" + idid).click();
  31. $("#" + idid).change(function (e) {
  32. console.log('change事件', e);
  33. console.log(this)
  34. var path= getImgPath(this.files[]);
  35. console.log("--------"+path);
  36. var arr = path.split("/");
  37. var strPath='--------:null/'+arr[arr.length-];
  38. console.log(strPath)
  39. var a=createImg(path,idid);
  40. $("#em").append(a)
  41. });
  42. var newline = document.createElement("br");//创建一个BR标签是为能够换行!
  43. $("#fileId").append(newline);
  44. }
  45. //动态显示上传图片
  46. function uploadImg(path) {
  47. var imgDiv = $("#img-con");
  48. var $img = $("<img/>");
  49. $img.attr("src", path);
  50. imgDiv.append($img);
  51. }
  52. //获取要上传单张图片的本地路径
  53. function getImgPath(file) {
  54. var url = null;
  55. if(window.createObjectURL != undefined) { // basic
  56. url = window.createObjectURL(file);
  57. } else if(window.URL != undefined) { // mozilla(firefox)
  58. url = window.URL.createObjectURL(file);
  59. } else if(window.webkitURL != undefined) { // webkit or chrome
  60. url = window.webkitURL.createObjectURL(file);
  61. }
  62. return url;
  63. }
  64. function createImg(src,idid) {
  65. var box = $("<div class='img-box uploadfile'>");
  66. var newImg = document.createElement("img");
  67. newImg.src=src;
  68. newImg.id="img"+idid;
  69. newImg.height=;
  70. newImg.width=;
  71. newImg.onclick='showImagePopup(\"" + src + "\")';
  72. //box.append("<img src='" + src + "' height='100px' width='100px' onclick='showImagePopup(\"" + src + "\")'>");
  73. box.append(newImg);
  74. return box;
  75. }
  76. function showImagePopup(src) {
  77. if (getClass(src) === "String") {
  78. var popup = $("<img></img");
  79. popup.addClass("image-popup").attr("src", src);
  80. var shade = $("<div></div>").addClass("shade");
  81. $(document.body).append(shade.append(popup));
  82. shade.click(function () {
  83. $(this).remove();
  84. });
  85. popup.fadeIn();
  86. // popup.click(function() {
  87. // window.event ? window.event.cancelBubble = true :
  88. // window.event.stopPropagation();
  89. // });
  90. }
  91. }
  92. </script>
  93. </html>

 

以上就是html多文件上传,可支持预览的详细内容,更多关于html多文件上传,可支持预览的资料请关注九品源码其它相关文章!