PHP群:95885625 Hbuilder+MUI群:81989597 站长QQ:634381967
    您现在的位置: 首页 > 开发编程 > HBuilderX教程 > 正文

    hbuilder mui uploader图片上传到服务器完整版(ASP.NET)

    作者:admin来源:网络浏览:时间:2020-09-30 00:07:50我要评论
    导读:html布局,比较简单,模仿微信的:<divclass="dynamic_images"><ul><!--<li><imgsrc="images/cbd.jpg"></li>--><li><imgsrc="images/iconfo...
    html布局,比较简单,模仿微信的:
    1. <div class="dynamic_images"> 
    2.     <ul> 
    3.         <!--<li><img src="images/cbd.jpg"></li>--> 
    4.         <li><img src="images/iconfont-tianjia.png" id="addnew"></li> 
    5.         <!--<li><span class="mui-icon iconfont icon-jia2" style="font-size: 28px;"></span></li>--> 
    6.  
    7.     </ul> 
    8. </div> 
    页面初始化的一些操作:
    1. document.addEventListener("plusready", plusReady, false); 
    2.  
    3. function plusReady() { 
    4.     document.getElementById("addnew").addEventListener("tap"function() { 
    5.         showActionSheet();//拍照还是相册 
    6.     }); 
    7.     document.getElementById("fabiao").addEventListener("tap"function() { 
    8.         upload();//上传文件 
    9.     }); 
    10.     plus.nativeUI.closeWaiting(); 
    11.  
    页面中js部分
    1. var f1 = null
    2. var picarr = new Array(); 
    3. var basearr = new Array(); 
    4. var lat = ""
    5.     longt = ""
    6. var files = []; 
    7. // 上传文件 
    8. function upload() { 
    9.      
    10.      
    11.     var wt = plus.nativeUI.showWaiting(); 
    12.     var task = plus.uploader.createUpload(server + "?action=dynamicadd", { 
    13.             method: "POST" 
    14.         }, 
    15.         function(t, status) { //上传完成 
    16.             if (status == 200) { 
    17. //                      console.log("上传成功:" + t.responseText); 
    18.                 mui.toast("发表成功"); 
    19.                 //插入本地数据库 
    20.                 wt.close(); 
    21.                 mui.back(); 
    22.             } else { 
    23.                 console.log("上传失败:" + status); 
    24.                 wt.close(); 
    25.             } 
    26.         } 
    27.     ); 
    28.     var title = $("#tbxtitle").val(); 
    29.     if (title.length < 1) { 
    30.         wt.close(); 
    31.         mui.toast("内容不能为空"); 
    32.     } else {  
    33.         task.addData("title", title); 
    34.         task.addData("uid", getUid()); 
    35.         task.addData("userid", plus.storage.getItem("policeid")); 
    36.         //task.addData("lat", lat.toString()); 
    37.         //task.addData("longt", longt.toString()); 
    38. //              console.log("准备上传"+files.length+"个图片"); 
    39.         for (var i = 0; i < files.length; i++) { 
    40.             var f = files[i]; 
    41. //                  console.log("准备上传的图片路径:"+f.path); 
    42.             task.addFile(f.path, { 
    43.                 key: f.name 
    44.             }); 
    45.         } 
    46.         task.start(); 
    47.     } 
    48.      
    49.  
    50. // 添加文件 
    51. var index = 1; 
    52. var newUrlAfterCompress; 
    53. function appendFile(p) { 
    54.     files.push({ 
    55.         name: "uploadkey" + index,//这个值服务器会用到,作为file的key 
    56.         path: p 
    57.     }); 
    58.     index++; 
    59. // 产生一个随机数 
    60. function getUid() { 
    61.     return Math.floor(Math.random() * 100000000 + 10000000).toString(); 
    62.  
    63. function getposition() { 
    64.     plus.geolocation.getCurrentPosition(function(p) { 
    65.         var codns = p.coords; // 获取地理坐标信息; 
    66.         lat = codns.latitude; //获取到当前位置的纬度; 
    67.         longt = codns.longitude; //获取到当前位置的经度 
    68.     }, function(e) { 
    69.         //alert("获取百度定位位置信息失败:" + e.message); 
    70.     }, { 
    71.         provider: 'baidu' 
    72.     }); 
    73.  
    74. function galleryImgs() { // 从相册中选择图片 
    75.     plus.gallery.pick(function(e) { 
    76.         $(".dynamic_images ul li").remove(".pickimg"); 
    77. //              console.log("选择了"+e.files.length+"个图片"); 
    78.         for (var i = 0; i < e.files.length; i++) { 
    79.             if (i < 9) { 
    80.                 picarr[i] = e.files[i]; 
    81.                 $(".dynamic_images ul").prepend("<li class='pickimg'><img src='" + e.files[i] + "' /></li>"); 
    82.                 var dstname="_downloads/"+getUid()+".jpg";//设置压缩后图片的路径 
    83.                 newUrlAfterCompress=compressImage(e.files[i],dstname); 
    84.                 appendFile(dstname); 
    85.                 //console.log(e.files[i]); 
    86.                 //console.log(dstname); 
    87.             } 
    88.         } 
    89.     }, function(e) { 
    90.         console.log("取消选择图片"); 
    91.     }, { 
    92.         filter: "image"
    93.         multiple: true 
    94.     }); 
    95.  
    96. //压缩图片,这个比较变态的方法,无法return 
    97. function compressImage(src,dstname) { 
    98.     //var dstname="_downloads/"+getUid()+".jpg"; 
    99.     plus.zip.compressImage({ 
    100.             src: src, 
    101.             dst: dstname, 
    102.             overwrite:true
    103.             quality: 20 
    104.         }, 
    105.         function(event) { 
    106.             //console.log("Compress success:"+event.target); 
    107.             return event.target; 
    108.         }, 
    109.         function(error) { 
    110.             console.log(error); 
    111.             return src; 
    112.             //alert("Compress error!"); 
    113.         }); 
    114.      
    115. //旋转图片,本文没用到 
    116. function rotateImage() { 
    117.     plus.zip.compressImage({ 
    118.             src: "_www/a.jpg"
    119.             dst: "_doc/a.jpg"
    120.             rotate: 90 // 旋转90度 
    121.         }, 
    122.         function() { 
    123.             alert("Compress success!"); 
    124.         }, 
    125.         function(error) { 
    126.             alert("Compress error!"); 
    127.         }); 
    128.  
    129. function showActionSheet() { 
    130.     var bts = [{ 
    131.         title: "拍照" 
    132.     }, { 
    133.         title: "从相册选择" 
    134.     }]; 
    135.     plus.nativeUI.actionSheet({ 
    136.             cancel: "取消"
    137.             buttons: bts 
    138.         }, 
    139.         function(e) { 
    140.             if (e.index == 1) { 
    141.                 getImage(); 
    142.             } else if (e.index == 2) { 
    143.                 galleryImgs(); 
    144.             } 
    145.         } 
    146.     ); 
    147. //拍照 
    148. function getImage() { 
    149.     var cmr = plus.camera.getCamera(); 
    150.     cmr.captureImage(function(p) { 
    151.         plus.io.resolveLocalFileSystemURL(p, function(entry) { 
    152.             var localurl = entry.toLocalURL(); // 
    153.             $(".dynamic_images ul li").remove(".pickimg"); 
    154.             $(".dynamic_images ul").prepend("<li class='pickimg'><img src='" + localurl + "' /></li>"); 
    155.         }); 
    156.     }); 
    服务端asp.net版
    1. string file = ""
    2. int count = Request.Files.Count; 
    3.  
    4. for (int i = 0; i < count; i++) 
    5.     int l = Request.Files["uploadkey" + (i + 1)].ContentLength; 
    6.     byte[] buffer = new byte[l]; 
    7.     Stream s = Request.Files["uploadkey" + (i + 1)].InputStream; 
    8.     System.Drawing.Bitmap image = new System.Drawing.Bitmap(s); 
    9.     string imgname = Common.GetGuid() + ".jpg"
    10.     string path = "Images/" + DateTime.Now.ToString("yyyyMMdd") + "/"
    11.     if (!Directory.Exists(HttpContext.Current.Server.MapPath(path))) 
    12.     { 
    13.         System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(path)); 
    14.     } 
    15.     image.Save(Server.MapPath(path + "/" + imgname)); 
    速度非常快,测试过,如果不压缩速度没有明显的下降,但是考虑到显示时候加载的问题,还是进行了压缩。
    转载请注明(B5教程网)原文链接:https://b5.mxunkeji.com/content-146-4937-1.html
    相关热词搜索: hbuilder mui 图片上传