阅读 106

文件以字节的形式上传到服务器接口

  1  string name = @"D:\Img\AAEON.png";
  2   File file = new File()
  3                     {
  4                         Bytes = getImageByte(name),
  5                         FileStream=null,
  6                     };
  7    string httpurl = "http://localhost:51843/customerapi/Other/FileImg";
  8                     string content = Newtonsoft.Json.JsonConvert.SerializeObject(file);
  9                     var result = Post(httpurl, content, "", null);
 10 
 11 
 12   /// 
 13         /// 根据图片路径返回图片的字节流byte[]
 14         /// 
 15         /// 图片路径
 16         /// 返回的字节流
 17         private static byte[] getImageByte(string imagePath)
 18         {
 19             FileStream files = new FileStream(imagePath, FileMode.Open);
 20             byte[] imgByte = new byte[files.Length];
 21             files.Read(imgByte, 0, imgByte.Length);
 22             files.Close();
 23             return imgByte;
 24         }
 25 
 26   public class File
 27         {
 28             /// 
 29             /// 字节
 30             /// 
 31             public byte[] Bytes { get; set; }
 32 
 33             /// 
 34             /// 
 35             /// 
 36             public Stream FileStream { get; set; }
 37 
 38             /// 
 39             /// 文件名称
 40             /// 
 41             public string FileName { get; set; }
 42             /// 
 43             /// 大小限制,单位KB
 44             /// 
 45             public int MaxSize { get; set; }
 46         }
 47 
 48 
 49   /// 
 50         /// 
 51         /// 
 52         /// 
 53         /// 
 54         /// 
 55         /// 
 56         /// 
 57         public static string Post(string url, string content, string ContentType, Encoding requestEncoding, string Method = "POST")
 58         {
 59             string result = string.Empty;
 60             HttpWebRequest req;
 61             ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3
 62                                   | SecurityProtocolType.Tls
 63                                   | (SecurityProtocolType)0x300 //Tls11
 64                                   | (SecurityProtocolType)0xC00; //Tls12
 65 
 66             if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
 67             {
 68                 ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
 69                 req = WebRequest.Create(url) as HttpWebRequest;
 70                 req.ProtocolVersion = HttpVersion.Version10;
 71             }
 72             else
 73             {
 74                 req = WebRequest.Create(url) as HttpWebRequest;
 75             }
 76             req.Method = Method;
 77             req.Timeout = 20000;
 78             req.ReadWriteTimeout = 32000;
 79             if (string.IsNullOrEmpty(ContentType))
 80             {
 81                 req.ContentType = "application/json; charset=utf-8";
 82             }
 83             else
 84             {
 85                 req.ContentType = ContentType;
 86             }
 87 
 88             req.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36";
 89             req.Accept = "application/json, text/javascript, */*; q=0.01";
 90 
 91 
 92             #region 添加Post 参数
 93             try
 94             {
 95                 if (requestEncoding == null) requestEncoding = Encoding.UTF8;
 96                 byte[] data = requestEncoding.GetBytes(content.ToString());
 97 
 98                 req.ContentLength = data.Length;
 99                 using (Stream reqStream = req.GetRequestStream())
100                 {
101                     reqStream.Write(data, 0, data.Length);
102                     reqStream.Close();
103                 }
104             }
105             catch (Exception ex)
106             {
107             }
108 
109             #endregion
110 
111             try
112             {
113                 HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
114                 Stream stream = resp.GetResponseStream();
115                 //获取响应内容
116                 using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
117                 {
118                     result = reader.ReadToEnd();
119                 }
120             }
121             catch (Exception ex)
122             {
123             }
124             return result;
125         }
将图片转成字节 POST请求接口
 1  /// 
 2         /// 图片上传到服务上
 3         /// 
 4         /// 
 5         /// 
 6         public Result<string> FileImg(HqewApi.Customer.Model.File files)
 7         {
 8             Result<string> result1 = new Result<string>();
 9             if (files == null)
10             {
11                 result1.StatusCode = 100;
12                 result1.Message = "图片流为空";
13                 return result1;
14             }
15 //此处自行修改,上传到服务器
16             MemoryStream image = byteArrayToImage(files.Bytes);
17             UploadFile file = new UploadFile();
18             FileM info = new FileM() { FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".png", FileStream = image, FileType = 1 };
19             var result = file.UploadToFDFS(info);
20             if (result.IsSuccessed)
21             {
22                 result1.StatusCode = 200;
23                 result1.Message = "成功";
24                 result1.Ext1 = result.FileUrl;
25             }
26             else
27             {
28                 result1.StatusCode = 100;
29                 result1.Message = result.ErrorMsg;
30             }
31             return result1;
32         }
33 
34 
35         /// 
36         /// 字节数组生成流
37         /// 
38         /// 字节数组
39         /// 图片
40         private MemoryStream byteArrayToImage(byte[] Bytes)
41         {
42             MemoryStream ms = new MemoryStream(Bytes);
43             return ms;
44         }
45 
46 
47 
48  [Serializable]
49     public class Result
50     {
51         /// 
52         /// 状态码 100失败 200成功
53         /// 
54         public int StatusCode { get; set; }
55 
56         /// 
57         /// 消息
58         /// 
59         public string Message { get; set; }
60     }
61 
62     [Serializable]
63     public class Result : Result
64     {
65         /// 
66         /// 扩展字段
67         /// 
68         public T Ext1 { get; set; }
69     }
后端接口接收字节

 

原文:https://www.cnblogs.com/www-mcl-1234/p/15213235.html

文章分类
代码人生
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐