导读:html布局,比较简单,模仿微信的:<divclass="dynamic_images"><ul><!--<li><imgsrc="images/cbd.jpg"></li>--><li><imgsrc="images/iconfo...
html布局,比较简单,模仿微信的:
- <div class="dynamic_images">
- <ul>
-
- <li><img src="images/iconfont-tianjia.png" id="addnew"></li>
-
-
- </ul>
- </div>
页面初始化的一些操作:
- document.addEventListener("plusready", plusReady, false);
-
- function plusReady() {
- document.getElementById("addnew").addEventListener("tap", function() {
- showActionSheet();
- });
- document.getElementById("fabiao").addEventListener("tap", function() {
- upload();
- });
- plus.nativeUI.closeWaiting();
-
- }
页面中js部分
- var f1 = null;
- var picarr = new Array();
- var basearr = new Array();
- var lat = "",
- longt = "";
- var files = [];
-
- function upload() {
-
-
- var wt = plus.nativeUI.showWaiting();
- var task = plus.uploader.createUpload(server + "?action=dynamicadd", {
- method: "POST"
- },
- function(t, status) {
- if (status == 200) {
-
- mui.toast("发表成功");
-
- wt.close();
- mui.back();
- } else {
- console.log("上传失败:" + status);
- wt.close();
- }
- }
- );
- var title = $("#tbxtitle").val();
- if (title.length < 1) {
- wt.close();
- mui.toast("内容不能为空");
- } else {
- task.addData("title", title);
- task.addData("uid", getUid());
- task.addData("userid", plus.storage.getItem("policeid"));
-
-
-
- for (var i = 0; i < files.length; i++) {
- var f = files[i];
-
- task.addFile(f.path, {
- key: f.name
- });
- }
- task.start();
- }
-
- }
-
-
- var index = 1;
- var newUrlAfterCompress;
- function appendFile(p) {
- files.push({
- name: "uploadkey" + index,
- path: p
- });
- index++;
- }
-
- function getUid() {
- return Math.floor(Math.random() * 100000000 + 10000000).toString();
- }
-
- function getposition() {
- plus.geolocation.getCurrentPosition(function(p) {
- var codns = p.coords;
- lat = codns.latitude;
- longt = codns.longitude;
- }, function(e) {
-
- }, {
- provider: 'baidu'
- });
- }
-
- function galleryImgs() {
- plus.gallery.pick(function(e) {
- $(".dynamic_images ul li").remove(".pickimg");
-
- for (var i = 0; i < e.files.length; i++) {
- if (i < 9) {
- picarr[i] = e.files[i];
- $(".dynamic_images ul").prepend("<li class='pickimg'><img src='" + e.files[i] + "' /></li>");
- var dstname="_downloads/"+getUid()+".jpg";
- newUrlAfterCompress=compressImage(e.files[i],dstname);
- appendFile(dstname);
-
-
- }
- }
- }, function(e) {
- console.log("取消选择图片");
- }, {
- filter: "image",
- multiple: true
- });
- }
-
-
- function compressImage(src,dstname) {
-
- plus.zip.compressImage({
- src: src,
- dst: dstname,
- overwrite:true,
- quality: 20
- },
- function(event) {
-
- return event.target;
- },
- function(error) {
- console.log(error);
- return src;
-
- });
-
- }
-
- function rotateImage() {
- plus.zip.compressImage({
- src: "_www/a.jpg",
- dst: "_doc/a.jpg",
- rotate: 90
- },
- function() {
- alert("Compress success!");
- },
- function(error) {
- alert("Compress error!");
- });
- }
-
- function showActionSheet() {
- var bts = [{
- title: "拍照"
- }, {
- title: "从相册选择"
- }];
- plus.nativeUI.actionSheet({
- cancel: "取消",
- buttons: bts
- },
- function(e) {
- if (e.index == 1) {
- getImage();
- } else if (e.index == 2) {
- galleryImgs();
- }
- }
- );
- }
-
- function getImage() {
- var cmr = plus.camera.getCamera();
- cmr.captureImage(function(p) {
- plus.io.resolveLocalFileSystemURL(p, function(entry) {
- var localurl = entry.toLocalURL();
- $(".dynamic_images ul li").remove(".pickimg");
- $(".dynamic_images ul").prepend("<li class='pickimg'><img src='" + localurl + "' /></li>");
- });
- });
- }
服务端asp.net版
- string file = "";
- int count = Request.Files.Count;
-
- for (int i = 0; i < count; i++)
- {
- int l = Request.Files["uploadkey" + (i + 1)].ContentLength;
- byte[] buffer = new byte[l];
- Stream s = Request.Files["uploadkey" + (i + 1)].InputStream;
- System.Drawing.Bitmap image = new System.Drawing.Bitmap(s);
- string imgname = Common.GetGuid() + ".jpg";
- string path = "Images/" + DateTime.Now.ToString("yyyyMMdd") + "/";
- if (!Directory.Exists(HttpContext.Current.Server.MapPath(path)))
- {
- System.IO.Directory.CreateDirectory(HttpContext.Current.Server.MapPath(path));
- }
- image.Save(Server.MapPath(path + "/" + imgname));
- }
速度非常快,测试过,如果不压缩速度没有明显的下降,但是考虑到显示时候加载的问题,还是进行了压缩。