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 /// <summary> 13 /// 根据图片路径返回图片的字节流byte[] 14 /// </summary> 15 /// <param name="imagePath">图片路径</param> 16 /// <returns>返回的字节流</returns> 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 /// <summary> 29 /// 字节 30 /// </summary> 31 public byte[] Bytes { get; set; } 32 33 /// <summary> 34 /// 35 /// </summary> 36 public Stream FileStream { get; set; } 37 38 /// <summary> 39 /// 文件名称 40 /// </summary> 41 public string FileName { get; set; } 42 /// <summary> 43 /// 大小限制,单位KB 44 /// </summary> 45 public int MaxSize { get; set; } 46 } 47 48 49 /// <summary> 50 /// 51 /// </summary> 52 /// <param name="url"></param> 53 /// <param name="content"></param> 54 /// <param name="ContentType"></param> 55 /// <param name="requestEncoding"></param> 56 /// <returns></returns> 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 }
1 /// <summary> 2 /// 图片上传到服务上 3 /// </summary> 4 /// <param name="FileStream"></param> 5 /// <returns></returns> 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 /// <summary> 36 /// 字节数组生成流 37 /// </summary> 38 /// <param name="Bytes">字节数组</param> 39 /// <returns>图片</returns> 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 /// <summary> 52 /// 状态码 100失败 200成功 53 /// </summary> 54 public int StatusCode { get; set; } 55 56 /// <summary> 57 /// 消息 58 /// </summary> 59 public string Message { get; set; } 60 } 61 62 [Serializable] 63 public class Result<T> : Result 64 { 65 /// <summary> 66 /// 扩展字段 67 /// </summary> 68 public T Ext1 { get; set; } 69 }