indexbf.html 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no,viewport-fit=cover" />
  6. <meta name="apple-mobile-web-app-capable" content="yes" />
  7. <meta name="apple-mobile-web-app-status-bar-style" content="black" />
  8. </head>
  9. <body>
  10. <style>
  11. .dynamic_images ul {
  12. margin: 0;
  13. padding: 0;
  14. }
  15. .dynamic_images ul li {
  16. position: relative;
  17. display: inline-block;
  18. margin-top: 10px;
  19. margin-bottom: 10px;
  20. margin-right: 10px;
  21. float: left;
  22. list-style: none;
  23. }
  24. .dynamic_images ul li img {
  25. width: 50px;
  26. height: 50px;
  27. border-radius: 5px;
  28. }
  29. .del {
  30. display: inline-block;
  31. position: absolute;
  32. top:6px;
  33. right: 0px;
  34. width: 12px;
  35. height: 2px;
  36. background: #ED544D;
  37. line-height: 0;
  38. font-size: 0;
  39. vertical-align: middle;
  40. -webkit-transform: rotate(45deg);
  41. }
  42. .del:after {
  43. content: "/";
  44. display: block;
  45. width: 12px;
  46. height: 2px;
  47. background: #ED544D;
  48. -webkit-transform: rotate(-90deg);
  49. }
  50. </style>
  51. <header class="mui-bar mui-bar-nav" >
  52. <a class="mui-action-back mui-icon mui-icon-left-nav mui-pull-left"></a>
  53. <div class="mui-title">图片预览上传</div>
  54. </header>
  55. <div class="mui-content" >
  56. <form id='from1' enctype = "multipart/form-data">
  57. <div style="margin-top: 16px;margin-bottom: 10px;">
  58. <span class="title">添加照片(选填)</span>
  59. <span id="picnum" style="float: right;margin-right: 20px;font-size: 12px; color: #666666;">0/9</span>
  60. </div>
  61. <div class="dynamic_images" style="background-color: #ffffff;height: 70px;width: 100%;padding-left: 15px;padding-right: 15px;">
  62. <ul class="row mui-input-row">
  63. <li>
  64. <img src="img/iconfont-tianjia.png" id="addnew" onclick="showActionSheet();" />
  65. </li>
  66. </ul>
  67. </div>
  68. <div style="margin-top: 16px;width: 100%;text-align: center;">
  69. <button style="background-color: #169BD5;height: 40px;width: 266px;font-size: 14px;color: #FFFFFF;border-radius: 5px;border: 0px;" type="button" onclick="submits()">提交</button>
  70. </div>
  71. </form>
  72. </div>
  73. </body>
  74. <script src="js/mui.min.js"></script>
  75. <script src="js/jquery.min.js"></script>
  76. <script type="text/javascript">
  77. mui.init({
  78. swipeBack:true //启用右滑关闭功能
  79. });
  80. function submits(){
  81. var url='http://139.196.105.31:8881/upload/multipleImageUpload'
  82. task = plus.uploader.createUpload( url,
  83. { method:"POST"},
  84. function (t, status ) {
  85. // 上传完成
  86. if ( status == 200 ) {
  87. // wt.close();
  88. console.log( "Upload success: " + JSON.stringify(t));
  89. } else {
  90. //mui.alert();
  91. //wt.close();
  92. console.log( "Upload failed: " +JSON.stringify(status) );
  93. }
  94. });
  95. for(var i = 0; i < files.length; i++) {
  96. var f = files[i];
  97. var j = i + 1;
  98. var temp = 'files' + j;
  99. task.addFile(f.path,{
  100. key: temp
  101. });
  102. }
  103. task.addData('name',name);
  104. task.start();
  105. // plus.nativeUI.showWaiting();
  106. }
  107. var files = [];
  108. // 添加文件
  109. var index = 1;
  110. var newUrlAfterCompress;
  111. function appendFile(p) {
  112. files.push({
  113. name: "files" + index,//这个值服务器会用到,作为file的key
  114. path: p
  115. });
  116. index++;
  117. }
  118. function showActionSheet() {
  119. if($(".dynamic_images ul li").length<10){
  120. var bts = [{
  121. title: "拍照"
  122. }, {
  123. title: "从相册选择"
  124. }];
  125. plus.nativeUI.actionSheet({
  126. cancel: "取消",
  127. buttons: bts
  128. },
  129. function(e) {
  130. if (e.index == 1) {
  131. getImage();
  132. } else if (e.index == 2) {
  133. galleryImgs();
  134. }
  135. }
  136. );
  137. }else{
  138. plus.nativeUI.alert('最多上传四张图片!');
  139. }
  140. }
  141. // 产生一个随机数
  142. function getUid() {
  143. return Math.floor(Math.random() * 100000000 + 10000000).toString();
  144. }
  145. //拍照
  146. function getImage() {
  147. var cmr = plus.camera.getCamera();
  148. cmr.captureImage(function(p) {
  149. plus.io.resolveLocalFileSystemURL(p, function(entry) {
  150. var localurl = entry.toLocalURL(); //
  151. var name=localurl.substring(localurl.lastIndexOf("/")+1);
  152. $(".dynamic_images ul").prepend("<li onclick='delImg(this)'><img src='" + localurl + "' /><span class='del'></span></li>");
  153. var dstname="_downloads/"+name;//设置压缩后图片的路径
  154. newUrlAfterCompress=compressImage(localurl,dstname);
  155. appendFile(dstname);
  156. document.getElementById("picnum").innerText=files.length+'/9';
  157. });
  158. });
  159. }
  160. function galleryImgs() {
  161. // 从相册中选择图片
  162. var num=9;
  163. if(10-$(".dynamic_images ul li").length>=1){
  164. num=10-$(".dynamic_images ul li").length;
  165. }
  166. plus.gallery.pick(function(e) {
  167. for (var i = 0; i < e.files.length; i++) {
  168. var name=e.files[i].substring(e.files[i].lastIndexOf("/")+1);
  169. $(".dynamic_images ul").prepend(
  170. "<li onclick='delImg(this)'><img src='" + e.files[i] + "'/><span class='del'></span></li>");
  171. var dstname="_downloads/"+name;//设置压缩后图片的路径
  172. newUrlAfterCompress=compressImage(e.files[i],dstname);
  173. appendFile(dstname);
  174. }
  175. document.getElementById("picnum").innerText=files.length+'/9';
  176. }, function(e) {
  177. console.log("取消选择图片");
  178. }, {
  179. filter: "image",
  180. multiple: true,
  181. maximum:num,
  182. system:false,
  183. onmaxed:function(){
  184. plus.nativeUI.alert('最多只能选择九张图片');
  185. }
  186. });
  187. }
  188. //压缩图片,这个比较变态的方法,无法return
  189. function compressImage(src,dstname) {
  190. //兼容
  191. if (0 != src.toString().indexOf("file://")) {
  192. src = "file://" + src;
  193. }
  194. plus.zip.compressImage({
  195. src: src,
  196. dst: dstname,
  197. overwrite:true,
  198. quality: 10
  199. },
  200. function(event) {
  201. //console.log("Compress success:"+event.target);
  202. return event.target;
  203. },
  204. function(error) {
  205. console.log(error);
  206. return src;
  207. });
  208. }
  209. function delImg(obj)
  210. {
  211. var list =document.getElementsByTagName('li');
  212. for(var i=0;i<list.length;i++){
  213. list[i].index=i;
  214. }
  215. var idx=2-obj.index;
  216. var ul=obj.parentNode;
  217. var btnArray=['是','否'];
  218. plus.nativeUI.confirm("确定要删除此图?",function(e){
  219. if(e.index==0){
  220. ul.removeChild(obj);
  221. files.splice(idx,1);
  222. document.getElementById("picnum").innerText=Number(files.length)+'/9';
  223. }
  224. },{"buttons":btnArray})
  225. }
  226. </script>
  227. </html>