zoukankan      html  css  js  c++  java
  • Layui上传图片 带接口

     layui.use('upload', function () {
        var upload = layui.upload;
    
        upload.render({
          elem: '#LAY_avatarUpload'
          , url: setter.apiDomain + '/api/WxPay/UploadFileByStream_Head'
          , choose: function (obj) {
            //将每次选择的文件追加到文件队列
            var files = obj.pushFile();
    
            //预读本地文件,如果是多文件,则会遍历。(不支持ie8/9)
            obj.preview(function (index, file, result) {
              console.log(index); //得到文件索引
              console.log(file); //得到文件对象
              console.log(result); //得到文件base64编码,比如图片
    
              //obj.resetFile(index, file, '123.jpg'); //重命名文件名,layui 2.3.0 开始新增
    
              //这里还可以做一些 append 文件列表 DOM 的操作
    
              //obj.upload(index, file); //对上传失败的单个文件重新上传,一般在某个事件中使用
              //delete files[index]; //删除列表中对应的文件,一般在某个事件中使用
            });
    
          }
          , done: function (index, res) {
            //上传完毕回调
            if (index.Data.Url != "") {
              var url = index.Data.Url;
              $(".UserHeadImgUrl").attr('src', setter.apiDomain + url);
            }
            var UserHeadImgUrl = $('.UserHeadImgUrl').attr("src");
            var url = '/api/Userinformation/EditImg';                  ----这个主要就是更改字段,就没粘上来
            var parment = {
              UserHeadImgUrl: UserHeadImgUrl
            }
            callpack = function ()
            {
    
            }
            admin.ajax(url, parment, callpack)
          }
          , error: function (index) {
            //请求异常回调
          }
        });
      })


    ------------接口
    UploadFileByStream_Head
    public ResultInfo<UploadFileResponseModel> UploadFileByStream_Head()
            {
                var info = new ResultInfo<UploadFileResponseModel>();
                try
                {
                    #region 校验数据
    
                    if (!Request.Content.IsMimeMultipartContent())
                    {
                        info.Result = false;
                        info.StatusCode = (int)StatusCodeEnum.HttpRequestError;
                        info.State = StatusCodeEnum.HttpRequestError.ToString();
                        info.Msg = "不是有效的上传方式";
    
                        return info;
                    }
    
                    if (HttpContext.Current.Request.Files.Count <= 0)
                    {
                        info.Result = false;
                        info.StatusCode = (int)StatusCodeEnum.HttpRequestError;
                        info.State = StatusCodeEnum.HttpRequestError.ToString();
                        info.Msg = "没有上传文件";
    
                        return info;
                    }
                    #endregion
    
                    var resInfo = new UploadFileResponseModel();
                    HttpPostedFile file = HttpContext.Current.Request.Files[0];
                    var ext = Path.GetExtension(file.FileName);
                    var fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "_" + Funcs.GetRandom(1000000, 10000000) + ext;
    
                    var filesName = DateTime.Now.ToString("yyyyMMddHH");
                    var url = "/Upload/Consignor/Img/" + filesName + "/" + fileName;
                    var path = HttpContext.Current.Server.MapPath("/Upload/Consignor/Img/" + filesName + "/");
                    Directory.CreateDirectory(path);
                    var fullFileName = path + fileName;
                    file.SaveAs(fullFileName);
    
                    //返回数据
                    resInfo.FileName = fileName;
                    resInfo.ImagesType = "接口上传Stream";
                    resInfo.LocalPath = file.FileName;
                    resInfo.Url = url;
                    info.Result = true;
                    info.StatusCode = (int)StatusCodeEnum.Success;
                    info.State = StatusCodeEnum.Success.ToString();
                    info.Data = resInfo;
                    info.Msg = "上传成功";
    
                    return info;
                }
                catch (Exception ex)
                {
                    string msg = "出现异常:" + ex.Message;
                    info.Result = false;
                    info.StatusCode = (int)StatusCodeEnum.Error;
                    info.State = StatusCodeEnum.Error.ToString();
                    info.Msg = msg;
                    return info;
                }
            };





  • 相关阅读:
    Azure 媒体服务的 RTMP 支持和实时编码器
    在_Linux_中如何使用_gdb_调试_C_程序
    你刚吃的兰州牛肉面,背后就藏着大数据
    《C++覆辙录》——1.9:使用糟糕的语言
    老司机带你用MaxCompute和表格存储玩转车联网数据
    快速部署rabbitMQ教程
    《第一本Docker书(修订版)》——1.3 能用Docker做什么
    《第一本Docker书(修订版)》——第1章_简介_1.1Docker简介
    【DockerCon2017最新技术解读】Docker最新特性介绍
    【DockerCon2017最新技术解读】如何在阿里云一键部署高可用的Kubernetes集群
  • 原文地址:https://www.cnblogs.com/lvqianqian/p/10057666.html
Copyright © 2011-2022 走看看