zoukankan      html  css  js  c++  java
  • 用户头像模块代码

    
    
    public async Task<ResultDto<ActionResult>> ImportCustomerLogo(string customerId, IFormFile file)
            {
                //var file = Request.Form.Files.FirstOrDefault();
                ResultDto<ActionResult> resultDto = new ResultDto<ActionResult>();
                CustomerDto customerDto = await _customerInfoService.GetCustomerInfoAsync(customerId);
                string[] allowFileType = { "jpg", "png" };
                if (!allowFileType.Contains(file.FileName.Substring(file.FileName.LastIndexOf(".") + 1, file.FileName.Length - file.FileName.LastIndexOf(".") - 1)))
                {
                    resultDto.Msg = "只支持jpg或png格式的图片上传";
                    return resultDto;
                }
                if (file.Length > 1024 * 1024)
                {
                    resultDto.Msg = "文件总大小不能超过1M";
                    return resultDto;
                }
                try
                {
                    FrontCustomerInfo frontCustomerInfo = new FrontCustomerInfo();
                    string path = Environment.CurrentDirectory + "\Uploader\CustomerLogo\" + customerDto.CustomerName + customerDto.CustomerId;
                    if (!Directory.Exists(path))
                    {
                        Directory.CreateDirectory(path);
                    }
                    //以字节数组写入文件
                    string filePath = path + "\" + file.FileName;
                    byte[] bytes;
                    using (var stream = file.OpenReadStream())
                    {
                        bytes = new byte[stream.Length];
                        var numToRead = stream.Length;
                        var numHasRead = 0;
                        do
                        {
                            int n = stream.Read(bytes, numHasRead, int.MaxValue);
                            numToRead -= n;
                            numHasRead += n;
                        }
                        while (numToRead > 0);
                    }
                    System.IO.File.WriteAllBytes(filePath, bytes);
                    //保存logo信息
                    frontCustomerInfo.CustomerId = customerId;
                    frontCustomerInfo.CustomerProfile = filePath;
                    await _customerInfoService.SaveFrontCustomerInfoAsync(frontCustomerInfo);
                    resultDto.IsSuccess = true;
                }
                catch (Exception ex)
                {
                    resultDto.Code = 500;
                    resultDto.Msg = ex.GetMsgDetail("上传头像出错:");
                    resultDto.IsSuccess = false;
                }
                return resultDto;
            }
    

      

    <button class="layui-btn  layui-btn-normal" style="margin:20px auto auto 20px;" onclick="selectFile()">选择多个文件</button>

    <form id="uploaderForm" action="/Manager/Employee/ImportEmployeePhotos" method="post" enctype="multipart/form-data" >
    <input id="uploader" type="file" name="uploader" hidden="hidden" multiple="multiple"/>
    </form>

    function selectFile() {
    $("#uploader").click();
    }


    $("#uploader").change(function () { if ($(this).length != 0) { LoadingHint(0); $("#uploaderForm").ajaxSubmit({ dataType: "json", resetForm: true, data: { employeeId: $.request("employeeId"), attachmentTypeEnum: $.request("attachmentType") }, success: function (data) { if (data.Status == "y") { dig.successcallback("导入成功!", function () { $("#uploader").val(""); getEmployeePhotoAlbum($.request("employeeId"), $.request("attachmentType")) }); } else { $("#uploader").val(""); layer.alert(data.Msg); } }, error: function (data) { layer.alert(data.responseJSON.Msg); $("#uploader").val(""); } }) } })

      

  • 相关阅读:
    Python之路第二篇——Python环境与安装
    div层、fieldset分组标签、table表格的居中特效的综合运用
    在不影响系统的情况下给C盘添加磁盘空间(分区工具)
    C# windowsFroms更换皮肤的简单使用
    第二代居民身份证阅读器GTICR100(国腾)接口类调用方法
    C# 指定字符串截取方法
    C# 报表(report)和LocalReport类如何实现打印?
    RewriterURL实现二级域名的访问
    如何修改VS2012产品使用权属于某某的名称?
    OS与Internet
  • 原文地址:https://www.cnblogs.com/zhangzhang1118/p/11934465.html
Copyright © 2011-2022 走看看