• 上传并压缩图片


    <script src="js/lrz.pc.min.js" type="text/javascript"></script>       //电脑端
    <script src="js/lrz.mobile.min.js" type="text/javascript"></script> //手机端

    //js代码 

    var filename = $("#fileName").val();

    var mime = filename.toLowerCase().substr(filename.lastIndexOf("."));

    if (mime == ".jpg" || mime == ".png" || mime == ".jpeg" || mime == ".gif") {
      var sizes = $("#" + c)[0].files[0].size;
      if (sizes > 1024 * 1024 * 5) {
      layer.msg("您上传的文件不能大于5M");
      return;
    }
    // loading层
    var index = layer.msg('加载中', { icon: 16, time: 30000 });
    lrz($("#fileName")[0].files[0], { quality: 0.5, height: 1500 }, function (rst) {
      var file = rst.base64;
      if (file.indexOf("image") >= 0) {
      var fd = new FormData();
      fd.append("fileToUpload", file);
      fd.append("CallType", "UploadImg");
      fd.append("strType", e);
      var xhr = new XMLHttpRequest();
      xhr.addEventListener("load", uploadComplete, false);
      xhr.open("POST", "/ashx/OrderNote.ashx", false);
      xhr.send(fd);
    }
      function uploadComplete(evt) {
        var data = evt.target.responseText;
        if (data != "") {
        ……………………
        }
        layer.close(index);
      }
    });
    } else {
      layer.msg("你上传的图片格式不正确,请重新选择!");
      return;
    }

    //一般处理程序OrderNote.ashx

    string datetimestring = DateTime.Now.ToString("yyyyMMddhhmmss");  

    var picbase = HttpContext.Current.Request.Form["fileToUpload"];
    if (picbase != null)
    {
      MemoryStream stream = new MemoryStream(Convert.FromBase64String(picbase.ToString().Replace("data:image/jpeg;base64,", "")));
      Bitmap img = new Bitmap(stream);
      if (!Directory.Exists(context.Server.MapPath("\assets\UploadImgs")))//如果不存在就创建file文件夹
      {
        Directory.CreateDirectory(context.Server.MapPath("\assets\UploadImgs"));
      }
      string imgpath = HttpContext.Current.Server.MapPath("\assets\UploadImgs\");//路径,相对于服务器当前的路径
      imgpath = String.Format("{0}{1}.jpg", imgpath, datetimestring);
      string XdPath = "assets/UploadImgs/" + datetimestring + ".jpg";
      img.Save(imgpath);

    }

    //另一种上传图片的方式

    //HttpPostedFile hpf = HttpContext.Current.Request.Files["fileToUpload"];//HttpPostedFile提供对客户端已上载的单独文件的访问
    //if (hpf != null)
    //{
    // string houzhui = Path.GetExtension(hpf.FileName);
    // if (!Directory.Exists(context.Server.MapPath("\assets\UploadImgs")))//如果不存在就创建file文件夹
    // {
    // Directory.CreateDirectory(context.Server.MapPath("\assets\UploadImgs"));
    // }
    // string savepath = context.Server.MapPath("\assets\UploadImgs\" + datetimestring + houzhui);//路径,相对于服务器当前的路径
    // string PicURL = "assets/UploadImgs/" + datetimestring + houzhui;
    // hpf.SaveAs(savepath);//保存

  • 相关阅读:
    jQuery封装函数
    jquery中DOM
    phpstorm软件配置端口问题
    键盘控制div移动并且解决停顿问题(原生js)
    js实现可视化区域内拖拽
    一张图轻松搞懂javascript event对象的clientX,offsetX,screenX,pageX区别
    js之鼠标随动后面跟随事件(类似于长龙跟着跑)
    js多物体运动之淡入淡出效果
    js实现网站首页分享滑块
    JS实现ul,li排序效果
  • 原文地址:https://www.cnblogs.com/gca123/p/6567973.html
Copyright © 2020-2023  润新知