fileImg.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // var webserviceUploadUrl="http://localhost:8881/upload/multipleImageUpload"
  2. var webserviceUploadUrl="http://180.163.105.110:8881/upload/multipleImageUpload";
  3. var myformList = [];
  4. var myformList2 = [];
  5. //上传图片的方法
  6. function loadUpImgs() {
  7. var myImgList = new Array();
  8. var fileObj = document.getElementById("imgUploads").files[0];
  9. var qualityimg;
  10. if(fileObj.size/1024 > 1025){
  11. qualityimg=0.2
  12. }else{
  13. qualityimg=1
  14. }
  15. var form = new FormData();
  16. var form2 = new FormData();
  17. photoCompress(fileObj, {
  18. quality: qualityimg
  19. }, function(base64Codes){
  20. myImgList.push(base64Codes);
  21. var lengli = $(".dynamic_images ul li").length
  22. if(lengli<5){
  23. var lengli = $(".dynamic_images ul li").length;
  24. var lengli1 = ($(".dynamic_images ul li").length-0)+1;
  25. $('#showimg').css("display","none");
  26. for(var i=0;i<myImgList.length;i++){
  27. var j=i+1
  28. $(".dynamic_images ul").prepend("<li id='files_"+lengli+"' onclick='delImg(this)'><img src='" + base64Codes + "' id='upimgpush"+i+"'/><span class='del'></span></li>");
  29. }
  30. document.getElementById("picnum").innerText=lengli1+'/5';
  31. var bl = convertBase64UrlToBlob(base64Codes);
  32. myformList2.push(base64Codes);
  33. form.append("file"+lengli, bl, "file_"+Date.parse(new Date())+".jpg"); // 文件对象
  34. form2.append("file", bl, "file_"+Date.parse(new Date())+".jpg"); // 文件对象
  35. }
  36. });
  37. myformList.push(form);
  38. }
  39. function loadUpImg() {
  40. // var imgesFile= document.getElementById("imgUpload").files;
  41. // console.log(imgesFile)
  42. // if(imgesFile.length==0){
  43. // return;
  44. // }
  45. var myImgList = new Array();
  46. var fileObj = document.getElementById("imgUpload").files[0];
  47. console.log(fileObj)
  48. var qualityimg;
  49. if(fileObj.size/1024 > 1025){
  50. qualityimg=0.2
  51. }else{
  52. qualityimg=1
  53. }
  54. var form = new FormData();
  55. var form2 = new FormData();
  56. photoCompress(fileObj, {
  57. quality: qualityimg
  58. }, function(base64Codes){
  59. // console.log(showSize(base64Codes))
  60. myImgList.push(base64Codes);
  61. // debugger
  62. //console.log(myImgList)
  63. var lengli = $(".dynamic_images ul li").length;
  64. if(lengli<10){
  65. var lengli = $(".dynamic_images ul li").length;
  66. var lengli1 = ($(".dynamic_images ul li").length-0)+1;
  67. $('#showimg').css("display","none");
  68. for(var i=0;i<myImgList.length;i++){
  69. var j=i+1
  70. $(".dynamic_images ul").prepend("<li id='files_"+lengli+"' onclick='delImg(this)'><img src='" + base64Codes + "' id='upimgpush"+i+"'/><span class='del'></span></li>");
  71. }
  72. document.getElementById("picnum").innerText=lengli1+'/5';
  73. // document.getElementById('imgbase64').src = base64Codes;
  74. //console.log("压缩后:" +base64Codes + " " );
  75. var bl = convertBase64UrlToBlob(base64Codes);
  76. myformList2.push(base64Codes);
  77. //console.log("file"+lengli)
  78. form.append("file"+lengli, bl, "file_"+Date.parse(new Date())+".jpg"); // 文件对象
  79. form2.append("file", bl, "file_"+Date.parse(new Date())+".jpg"); // 文件对象
  80. console.log(form)
  81. // $.ajax({
  82. // url: webserviceUploadUrl,
  83. // type: 'POST',
  84. // dateType: 'json',
  85. // contentType: "application/x-www-form-urlencoded; charset=utf-8",
  86. // data: form,
  87. // async: false,
  88. // cache: false,
  89. // contentType: false,
  90. // processData: false,
  91. // success: function (res) {
  92. // $(".dynamic_images ul").html("");
  93. // var data =res.responseText
  94. // localStorage.setItem("datas", data);
  95. // localStorage.setItem("adddataimg", data);
  96. // $("#picnum")[0].innerHTML='0/10';
  97. // $(".dynamic_images ul").append(
  98. // "<img src='img/show.png' id='showimg'/>");
  99. // // applyToDB();
  100. // // console.log(res[0].img)
  101. // //loadUpImg(i,sql);
  102. // }
  103. // });
  104. }
  105. });
  106. myformList.push(form) ;
  107. }
  108. function determineSubmit(){
  109. var n=0;
  110. var pushlist=[];
  111. for(var i=0;i<myformList.length;i++){
  112. debugger
  113. $.ajax({
  114. url: webserviceUploadUrl,
  115. type: 'POST',
  116. dateType: 'json',
  117. contentType: "application/x-www-form-urlencoded; charset=utf-8",
  118. data:myformList[i],
  119. async: false,
  120. cache: false,
  121. contentType: false,
  122. processData: false,
  123. success: function (res) {
  124. n++;
  125. pushlist.push(res[0])
  126. $(".dynamic_images ul").html("");
  127. $("#picnum")[0].innerHTML='0/5';
  128. $(".dynamic_images ul").append(
  129. "<img src='img/show.png' id='showimg'/>");
  130. if(n==myformList.length){
  131. var data =JSON.stringify(pushlist);
  132. localStorage.setItem("datas", data);
  133. localStorage.setItem("adddataimg", data);
  134. debugger
  135. applyToDBNew();
  136. }
  137. // console.log(res[0].img)
  138. //loadUpImg(i,sql);
  139. } ,
  140. error: function (xhr, textStatus, errorThrown) {
  141. /*错误信息处理*/
  142.         alert("进入error---");
  143.         alert("状态码:"+xhr.status);
  144.         alert("状态:"+xhr.readyState);//当前状态,0-未初始化,1-正在载入,2-已经载入,3-数据进行交互,4-完成。
  145.         alert("错误信息:"+xhr.statusText );
  146.         alert("返回响应信息:"+xhr.responseText );//这里是详细的信息
  147.         alert("请求状态:"+textStatus);         
  148.         alert(errorThrown);         
  149.         alert("请求失败");
  150. }
  151. });
  152. }
  153. myformList = [];
  154. }
  155. /*
  156. 三个参数
  157. file:一个是文件(类型是图片格式),
  158. w:一个是文件压缩的后宽度,宽度越小,字节越小
  159. objDiv:一个是容器或者回调函数
  160. photoCompress()
  161. */
  162. function photoCompress(file,w,objDiv){
  163. var ready=new FileReader();
  164. /*开始读取指定的Blob对象或File对象中的内容. 当读取操作完成时,readyState属性的值会成为DONE,如果设置了onloadend事件处理程序,则调用之.同时,result属性中将包含一个data: URL格式的字符串以表示所读取文件的内容.*/
  165. ready.readAsDataURL(file);
  166. ready.onload=function(){
  167. var re=this.result;
  168. canvasDataURL(re,w,objDiv)
  169. }
  170. }
  171. function canvasDataURL(path, obj, callback){
  172. var img = new Image();
  173. img.src = path;
  174. img.onload = function(){
  175. var that = this;
  176. // 默认按比例压缩
  177. var w = that.width,
  178. h = that.height,
  179. scale = w / h;
  180. w = obj.width || w;
  181. h = obj.height || (w / scale);
  182. var quality = 0.5; // 默认图片质量为0.7
  183. //生成canvas
  184. var canvas = document.createElement('canvas');
  185. var ctx = canvas.getContext('2d');
  186. // 创建属性节点
  187. var anw = document.createAttribute("width");
  188. anw.nodeValue = w;
  189. var anh = document.createAttribute("height");
  190. anh.nodeValue = h;
  191. canvas.setAttributeNode(anw);
  192. canvas.setAttributeNode(anh);
  193. ctx.drawImage(that, 0, 0, w, h);
  194. // 图像质量
  195. if(obj.quality && obj.quality <= 1 && obj.quality > 0){
  196. quality = obj.quality;
  197. }
  198. // quality值越小,所绘制出的图像越模糊
  199. var base64 = canvas.toDataURL('image/jpeg', quality);
  200. // 回调函数返回base64的值
  201. callback(base64);
  202. }
  203. }
  204. /**
  205. * 将以base64的图片url数据转换为Blob
  206. * @param urlData
  207. * 用url方式表示的base64图片数据
  208. */
  209. function convertBase64UrlToBlob(urlData){
  210. var arr = urlData.split(','), mime = arr[0].match(/:(.*?);/)[1],
  211. bstr = atob(arr[1]), n = bstr.length, u8arr = new Uint8Array(n);
  212. while(n--){
  213. u8arr[n] = bstr.charCodeAt(n);
  214. }
  215. return new Blob([u8arr], {type:mime});
  216. }
  217. function showSize(base64url) {
  218. //获取base64图片大小,返回MB数字
  219. var str = base64url.replace('data:image/jpeg;base64,', '');
  220. var equalIndex = str.indexOf('=');
  221. if(str.indexOf('=')>0) {
  222. str=str.substring(0, equalIndex);
  223. }
  224. var strLength=str.length;
  225. var fileLength=parseInt(strLength-(strLength/8)*2);
  226. console.log("```````````````"+strLength);
  227. // 由字节转换为MB
  228. var size = "";
  229. size = (fileLength/(1024 * 1024)).toFixed(2);
  230. console.log("```````````````"+size);
  231. var sizeStr = size + ""; //转成字符串
  232. var index = sizeStr.indexOf("."); //获取小数点处的索引
  233. var dou = sizeStr.substr(index + 1 ,2) //获取小数点后两位的值
  234. if(dou == "00"){ //判断后两位是否为00,如果是则删除00
  235. return sizeStr.substring(0, index) + sizeStr.substr(index + 3, 2)
  236. }
  237. return parseInt(size);
  238. }
  239. function delImg(obj)
  240. {
  241. var ul=obj.parentNode;
  242. var id = obj.id;
  243. // var id = obj.value;
  244. //console.log(obj)
  245. //console.log(myformList[0].get("file1"))
  246. var index=id.lastIndexOf("\_");
  247. id=id.substring(index+1,obj.length);
  248. myformList.splice(index, 1);
  249. console.log(id)
  250. // arrList.splice(2,1);
  251. ul.removeChild(obj);
  252. var lengli = $(".dynamic_images ul li").length;
  253. var lengli1 = ($(".dynamic_images ul li").length-0);
  254. document.getElementById("picnum").innerText=lengli1+'/5';
  255. if(lengli==0){
  256. $('#showimg').css("display","table-cell");
  257. }
  258. }