uploadImgs.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. var files = [];
  2. // 添加文件
  3. var index = 1;
  4. var newUrlAfterCompress;
  5. function uploadPicture(){
  6. var url='http://139.196.105.31:8881/upload/multipleImageUpload'
  7. task = plus.uploader.createUpload( url,
  8. { method:"POST"},
  9. function (t, status ) {
  10. // 上传完成
  11. if ( status == 200 ) {
  12. // wt.close();
  13. $(".dynamic_images ul").html("");
  14. var data =t.responseText
  15. localStorage.setItem("datas", data);
  16. localStorage.setItem("adddataimg", data);
  17. $("#picnum")[0].innerHTML='0/10';
  18. files=[];
  19. $(".dynamic_images ul").append(
  20. "<img src='img/show.png' id='showimg'/>");
  21. plus.nativeUI.alert('上传成功');
  22. applyToDB();
  23. } else {
  24. $(".dynamic_images ul").html("");
  25. $("#picnum")[0].innerHTML='0/10';
  26. $(".dynamic_images ul").append(
  27. "<img src='img/show.png' id='showimg'/>");
  28. plus.nativeUI.alert('上传失败');
  29. //mui.alert();
  30. //wt.close();
  31. console.log( "Upload failed: " +JSON.stringify(status) );
  32. }
  33. });
  34. for(var i = 0; i < files.length; i++) {
  35. var f = files[i];
  36. var j = i + 1;
  37. var temp = 'files' + j;
  38. task.addFile(f.path,{
  39. key: temp
  40. });
  41. }
  42. task.addData('name',name);
  43. task.start();
  44. // plus.nativeUI.showWaiting();
  45. }
  46. function appendFile(p) {
  47. files.push({
  48. name: "files" + index,//这个值服务器会用到,作为file的key
  49. path: p
  50. });
  51. index++;
  52. }
  53. function showActionSheet() {
  54. if($(".dynamic_images ul li").length<9){
  55. var bts = [{
  56. title: "拍照"
  57. }, {
  58. title: "从相册选择"
  59. }];
  60. plus.nativeUI.actionSheet({
  61. cancel: "取消",
  62. buttons: bts
  63. },
  64. function(e) {
  65. if (e.index == 1) {
  66. getImage();
  67. } else if (e.index == 2) {
  68. galleryImgs();
  69. }
  70. }
  71. );
  72. }else{
  73. plus.nativeUI.alert('最多上传十张图片!');
  74. }
  75. }
  76. // 产生一个随机数
  77. function getUid() {
  78. return Math.floor(Math.random() * 100000000 + 10000000).toString();
  79. }
  80. //拍照
  81. function getImage() {
  82. $('#showimg').css("display","none");
  83. var cmr = plus.camera.getCamera();
  84. cmr.captureImage(function(p) {
  85. plus.io.resolveLocalFileSystemURL(p, function(entry) {
  86. var localurl = entry.toLocalURL(); //
  87. var name=localurl.substring(localurl.lastIndexOf("/")+1);
  88. $(".dynamic_images ul").prepend("<li id='uploadliid' onclick='delImg(this)'><img src='" + localurl + "' /><span class='del'></span></li>");
  89. var dstname="_downloads/"+name;//设置压缩后图片的路径
  90. newUrlAfterCompress=compressImage(localurl,dstname);
  91. appendFile(dstname);
  92. document.getElementById("picnum").innerText=files.length+'/10';
  93. });
  94. });
  95. }
  96. function galleryImgs() {
  97. // 从相册中选择图片
  98. var num=9;
  99. if(10-$(".dynamic_images ul li").length>=1){
  100. num=10-$(".dynamic_images ul li").length;
  101. }
  102. plus.gallery.pick(function(e) {
  103. $('#showimg').css("display","none");
  104. for (var i = 0; i < e.files.length; i++) {
  105. var name=e.files[i].substring(e.files[i].lastIndexOf("/")+1);
  106. $(".dynamic_images ul").prepend(
  107. "<li id='uploadliid' onclick='delImg(this)'><img src='" + e.files[i] + "'/><span class='del'></span></li>");
  108. var dstname="_downloads/"+name;//设置压缩后图片的路径
  109. newUrlAfterCompress=compressImage(e.files[i],dstname);
  110. appendFile(dstname);
  111. }
  112. document.getElementById("picnum").innerText=files.length+'/10';
  113. }, function(e) {
  114. console.log("取消选择图片");
  115. }, {
  116. filter: "image",
  117. multiple: true,
  118. maximum:num,
  119. system:false,
  120. onmaxed:function(){
  121. plus.nativeUI.alert('最多只能选择十张图片');
  122. }
  123. });
  124. }
  125. //压缩图片,这个比较变态的方法,无法return
  126. function compressImage(src,dstname) {
  127. //兼容
  128. if (0 != src.toString().indexOf("file://")) {
  129. src = "file://" + src;
  130. }
  131. plus.zip.compressImage({
  132. src: src,
  133. dst: dstname,
  134. overwrite:true,
  135. quality: 9
  136. },
  137. function(event) {
  138. //console.log("Compress success:"+event.target);
  139. return event.target;
  140. },
  141. function(error) {
  142. console.log(error);
  143. return src;
  144. });
  145. }
  146. function delImg(obj)
  147. {
  148. var list =document.getElementsByTagName('li');
  149. for(var i=0;i<list.length;i++){
  150. list[i].index=i;
  151. }
  152. var idx=2-obj.index;
  153. var ul=obj.parentNode;
  154. var btnArray=['是','否'];
  155. plus.nativeUI.confirm("确定要删除此图?",function(e){
  156. if(e.index==0){
  157. ul.removeChild(obj);
  158. files.splice(idx,1);
  159. document.getElementById("picnum").innerText=Number(files.length)+'/10';
  160. }
  161. },{"buttons":btnArray})
  162. }