imgUpfile.js 14 KB

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