在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
string result=""; string urlUploadMultiFile = "http://127.0.0.1:8090/overload/uploaddata.action";//上传地址 //赋值 infoData.id="123"; infoData.name= "张三"; infoData.date= "2020-10-20 15:50:00"; 上传方法 /// <summary> /// 多文件上传 /// </summary> /// <param name="url">请求路径</param> /// <param name="jsonParam">数据</param> /// <param name="files">图片路径</param> /// <param name="contentType"></param> /// <param name="paramFileName">对应的图片字段</param> /// <returns></returns> public static int UploadMultiFile(string url, string jsonParam, string[] files, string contentType, string paramFileName) { int result = -1; if (files.Length <= 0) return result; Dictionary<string, string> nvc = new Dictionary<string, string>(); nvc.Add("queryParam", jsonParam); string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x");//边界符 byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");//开始边界符 HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url); wr.ContentType = "multipart/form-data; boundary=" + boundary; wr.Method = "POST"; wr.KeepAlive = true; wr.Credentials = System.Net.CredentialCache.DefaultCredentials; Stream rs = wr.GetRequestStream(); string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}"; foreach (string key in nvc.Keys) { rs.Write(boundarybytes, 0, boundarybytes.Length);//写入边界符 string formitem = string.Format(formdataTemplate, key, nvc[key]); byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem); rs.Write(formitembytes, 0, formitembytes.Length);//写入数据 } rs.Write(boundarybytes, 0, boundarybytes.Length);//写入边界符 byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n"); for (int k = 0, k2 = files.Length; k < k2; k++) { string fname = paramFileName; string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n"; string header = string.Format(headerTemplate, fname, files[k], contentType);//写入字符串 byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header); rs.Write(headerbytes, 0, headerbytes.Length);//写入文件 FileStream fileStream = new FileStream(files[k], FileMode.Open, FileAccess.Read); byte[] buffer = new byte[4096]; int bytesRead = 0; while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0) { rs.Write(buffer, 0, bytesRead); } fileStream.Close(); if (k < k2 - 1) rs.Write(boundarybytes, 0, boundarybytes.Length); } rs.Write(trailer, 0, trailer.Length); rs.Close(); WebResponse wresp = null; try { wresp = wr.GetResponse(); Stream stream2 = wresp.GetResponseStream(); StreamReader reader2 = new StreamReader(stream2); JObject resultJson = (JObject)JsonConvert.DeserializeObject(reader2.ReadToEnd()); if (resultJson.Property("message") != null && resultJson["message"].ToString() == "操作成功") { result = 1; SysCfg.WriteLog("result:" + resultJson, false); } //result = reader2.ReadToEnd(); } catch (Exception ex) { if (wresp != null) { wresp.Close(); wresp = null; } SysCfg.WriteLog("UploadMultiFile,ex:" + ex.Message); result = -1; } finally { wr = null; } return result; }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论