用户头像模块代码
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(""); } }) } })

浙公网安备 33010602011771号