123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- var files = [];
- // 添加文件
- var index = 1;
- var newUrlAfterCompress;
- function submits(){
- var url='http://139.196.105.31:8881/upload/multipleImageUpload'
- task = plus.uploader.createUpload( url,
- { method:"POST"},
- function (t, status ) {
- // 上传完成
- if ( status == 200 ) {
- // wt.close();
- $(".dynamic_images ul").html("");
-
- var data =t.responseText
- localStorage.setItem("datas", data);
- localStorage.setItem("adddataimg", data);
-
-
- $("#picnum")[0].innerHTML='0/10';
- files=[];
- $(".dynamic_images ul").append(
- "<img src='img/show.png' id='showimg'/>");
- plus.nativeUI.alert('上传成功');
- Addfuntion();
- } else {
- $(".dynamic_images ul").html("");
- $("#picnum")[0].innerHTML='0/10';
- $(".dynamic_images ul").append(
- "<img src='img/show.png' id='showimg'/>");
-
- plus.nativeUI.alert('上传失败');
- //mui.alert();
- //wt.close();
- console.log( "Upload failed: " +JSON.stringify(status) );
- }
- });
- for(var i = 0; i < files.length; i++) {
- var f = files[i];
- var j = i + 1;
- var temp = 'files' + j;
- task.addFile(f.path,{
- key: temp
- });
- }
-
- task.addData('name',name);
- task.start();
- // plus.nativeUI.showWaiting();
- }
-
-
- function appendFile(p) {
- files.push({
- name: "files" + index,//这个值服务器会用到,作为file的key
- path: p
- });
- index++;
- }
- function showActionSheet() {
- if($(".dynamic_images ul li").length<9){
- var bts = [{
- title: "拍照"
- }, {
- title: "从相册选择"
- }];
- plus.nativeUI.actionSheet({
- cancel: "取消",
- buttons: bts
- },
- function(e) {
- if (e.index == 1) {
- getImage();
- } else if (e.index == 2) {
- galleryImgs();
- }
- }
- );
- }else{
- plus.nativeUI.alert('最多上传十张图片!');
- }
- }
- // 产生一个随机数
- function getUid() {
- return Math.floor(Math.random() * 100000000 + 10000000).toString();
- }
- //拍照
- function getImage() {
- $('#showimg').css("display","none");
- var cmr = plus.camera.getCamera();
- cmr.captureImage(function(p) {
- plus.io.resolveLocalFileSystemURL(p, function(entry) {
- var localurl = entry.toLocalURL(); //
- var name=localurl.substring(localurl.lastIndexOf("/")+1);
- $(".dynamic_images ul").prepend("<li id='uploadliid' onclick='delImg(this)'><img src='" + localurl + "' /><span class='del'></span></li>");
- var dstname="_downloads/"+name;//设置压缩后图片的路径
- newUrlAfterCompress=compressImage(localurl,dstname);
- appendFile(dstname);
- document.getElementById("picnum").innerText=files.length+'/10';
- });
- });
- }
- function galleryImgs() {
- // 从相册中选择图片
- var num=9;
- if(10-$(".dynamic_images ul li").length>=1){
- num=10-$(".dynamic_images ul li").length;
- }
- plus.gallery.pick(function(e) {
-
- $('#showimg').css("display","none");
- for (var i = 0; i < e.files.length; i++) {
- var name=e.files[i].substring(e.files[i].lastIndexOf("/")+1);
- $(".dynamic_images ul").prepend(
- "<li id='uploadliid' onclick='delImg(this)'><img src='" + e.files[i] + "'/><span class='del'></span></li>");
- var dstname="_downloads/"+name;//设置压缩后图片的路径
- newUrlAfterCompress=compressImage(e.files[i],dstname);
- appendFile(dstname);
- }
- document.getElementById("picnum").innerText=files.length+'/10';
- }, function(e) {
- console.log("取消选择图片");
- }, {
- filter: "image",
- multiple: true,
- maximum:num,
- system:false,
- onmaxed:function(){
- plus.nativeUI.alert('最多只能选择十张图片');
- }
- });
- }
- //压缩图片,这个比较变态的方法,无法return
- function compressImage(src,dstname) {
- //兼容
- if (0 != src.toString().indexOf("file://")) {
- src = "file://" + src;
- }
- plus.zip.compressImage({
- src: src,
- dst: dstname,
- overwrite:true,
- quality: 50
- },
- function(event) {
- //console.log("Compress success:"+event.target);
- return event.target;
- },
- function(error) {
- console.log(error);
- return src;
- });
- }
- function delImg(obj)
- {
- var list =document.getElementsByTagName('li');
- for(var i=0;i<list.length;i++){
- list[i].index=i;
- }
- var idx=2-obj.index;
- var ul=obj.parentNode;
- var btnArray=['是','否'];
- plus.nativeUI.confirm("确定要删除此图?",function(e){
- if(e.index==0){
- ul.removeChild(obj);
- files.splice(idx,1);
- document.getElementById("picnum").innerText=Number(files.length)+'/10';
- }
- },{"buttons":btnArray})
- }
|