zoukankan      html  css  js  c++  java
  • 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         }
  • 相关阅读:
    为什么利用多个域名来存储网站资源会更有效?
    事件绑定和普通事件的区别
    浏览器地址栏输入一个URL后回车,将会发生的事情
    JS数据类型及数据转换
    JS中的NaN和isNaN
    大数据的结构和特征
    系统重装后,如何重新找回hexo+github搭建的博客
    javascript操作符
    html头部
    html中链接的使用方法及介绍
  • 原文地址:https://www.cnblogs.com/ljy0905/p/8668772.html
Copyright © 2011-2022 走看看