• winform httpclient 多文件上传


    客户端

     1                                         //此处放入循环中   
                              //此为服务端上传地址
                              string url = "http://localhost:62114/AppAreaName/DetectImage/UploadFile"; 2 string time = createDto.DetectionTime.ToString("yyyy-MM-dd-HH-mm"); 3 //图片名称 服务端用 4 string ss = "Default" + "_" + time + "_" + detectionCount + "_" + count + ".jpg"; 5 //string ss = (aa++).ToString() + " " + dia.ToString("f3") + " " + ld.ToString("f3") + " " + rr.ToString("f3") + ".bmp"; 6 //MessageBox.Show(ss); 7 //将图片数据流转换为图片并存储 8 Image img; 9 img = BytToImg(imageData); 10 Bitmap map = new Bitmap(img); 11 map.Save(path + "\" + ss); 12 //Stream imageStream= Stream.Null; 13 FileStream imageStream = new FileStream(path + "\" + ss, FileMode.Open); 14 content.Add(CreateFileContent(imageStream, ss, "image/jpeg")); 15 count++; 16 17 //using (var client = new HttpClient()) 18 //{ 19 // using (var content = new MultipartFormDataContent()) 20 // { 21 // content.Add(CreateFileContent(imageStream, ss, "image/jpeg")); 22 23 // var response = await client.PostAsync(url, content); 24 // response.EnsureSuccessStatusCode(); 25 // } 26 //}28 29 using (var client = new HttpClient()) 30 { 31 var response = await client.PostAsync(url, content); 32 response.EnsureSuccessStatusCode(); 33 }

    服务端代码

     1  [HttpPost]
     2         public async Task<JsonResult> UploadFile()
     3         {
     4             try
     5             {
     6                 var files = Request.Form.Files;
     7 
     8                 //Check input
     9                 if (files == null)
    10                 {
    11                     throw new UserFriendlyException(L("File_Empty_Error"));
    12                 }
    13                 int icount = 0;
    14                 foreach (var file in files)
    15                 {
    16                    
    17                     if (file.Length > 1048576) //1MB
    18                     {
    19                         throw new UserFriendlyException(L("File_SizeLimit_Error"));
    20                     }
    21                     if (string.IsNullOrEmpty(file.FileName))
    22                     {
    23                         throw new UserFriendlyException(L("File_Empty_Error"));
    24                     }
    25                     string[] fileNames = file.FileName.Split('_');
    26                     var fileObject = new DetectImages();
    27                     if (fileNames.Length == 4)
    28                     {
    29                         if (!DateTime.TryParse(fileNames[1], out var datetime))
    30                         {
    31                             datetime = DateTime.Now;
    32                         }
    33                         if (!int.TryParse(fileNames[2], out var count))
    34                         {
    35                             count = 0;
    36                         }
    37                         fileObject.DetectTime = datetime;
    38                         fileObject.DetectCount = count;
    39                     }
    40                     byte[] fileBytes;
    41                     using (var stream = file.OpenReadStream())
    42                     {
    43                         fileBytes = stream.GetAllBytes();
    44                     }
    45 
    46                     fileObject.ImageContent = fileBytes;
    47                     fileObject.DetectId = 0;
    48                     fileObject.TenantId = AbpSession.TenantId;
    49                     fileObject.ImageType = file.ContentType;
    50                     MemoryStream inputStream = new MemoryStream(fileBytes);
    51                     inputStream.Position = 0;
    52                     Image img = Image.FromStream(inputStream);
    53                     fileObject.ImageWidth = img.Size.Width;
    54                     fileObject.ImageHight = img.Size.Height;
    55                     inputStream.Dispose();
    56                     await _detectImagesManager.SaveAsync(fileObject);
    57                     icount++;
    58                 }
    59                 return Json(new AjaxResponse(new
    60                 {
    61                     count = icount
    62                 }));
    63             }
    64             catch (UserFriendlyException ex)
    65             {
    66                 return Json(new AjaxResponse(new ErrorInfo(ex.Message)));
    67             }
    68         }
  • 相关阅读:
    【作业】Python
    【作业】判断某个数是否是素数,返回结果
    【案例】Python之列表反转
    Python模块
    【作业】Python-数据转换:将列表["mo","deng","ge"]和[1,2,3] 转换成[("mo",1),("deng",2),("ge",3)]
    【个人笔记】Python-zip()函数
    【作业】Python-数据转换:将列表[3,7,0,5,1,8]中大于5元素置为0,小于5的元素置为1
    【作业】Python-将元组(1,2,3) 和集合{"four",5,6}合成一个列表
    【作业】Python-函数封装:交换两个变量的值
    【个人笔记】Python-sorted()函数
  • 原文地址:https://www.cnblogs.com/ljy0905/p/8668772.html
Copyright © 2020-2023  润新知