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         }

 

posted @ 2018-03-29 11:28  儿歌三百首  阅读(698)  评论(0编辑  收藏  举报