在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
一:用postman实现文件上传,需要配置的地方 1)参数的配置
2)headers的配置
3)body的配置
4)点击send就可以上传成功;
二: 用c#服务端接收 public async Task<IHttpActionResult> Test() { string result = ""; string url = ""; List<Stream> listStream = new List<Stream>(); List<byte[]> listContentByte = new List<byte[]>(); List<string> listFileName = new List<string>(); string methodsName = System.Web.HttpContext.Current.Request.QueryString["methodsName"]; string serverPath = System.Web.HttpContext.Current.Request.QueryString["serverPath"]; string ArtifactsNum = System.Web.HttpContext.Current.Request.QueryString["ArtifactsNum"]; string userName = System.Web.HttpContext.Current.Request.QueryString["userName"]; string guidStr = System.Web.HttpContext.Current.Request.QueryString["guidStr"]; string routeUrl = System.Web.HttpContext.Current.Request.QueryString["routeUrl"]; Encoding myEncoding = Encoding.GetEncoding("UTF-8"); //gb2312 FilesType filesType = FilesType.General; //接收传递过来的数据流 Stream stream = System.Web.HttpContext.Current.Request.InputStream; StreamReader reader = new StreamReader(stream, myEncoding); string xml = " "; StringBuilder strXML = new StringBuilder(); while (reader.Peek() >= 0) { string line = reader.ReadLine().Trim();//直接读取一行 if (line == null) return null; if (line == String.Empty) continue; if (line.StartsWith("<")) { xml = line.Trim(); strXML.Append(xml); } } String xmlData = reader.ReadToEnd(); listStream.Add(stream); //byte[] bytes = new byte[stream.Length]; //stream.Read(bytes, 0, bytes.Length); // 设置当前流的位置为流的开始 //stream.Seek(0, SeekOrigin.Begin); byte[] bytes = System.Text.Encoding.UTF8.GetBytes(strXML.ToString()); listContentByte.Add(bytes); Request.Content.ReadAsStreamAsync().Result.Seek(0, System.IO.SeekOrigin.Begin); ///string content = Request.Content.ReadAsStringAsync().Result; var httpRequest = HttpContext.Current.Request; //HttpFileCollection files = HttpContext.Current.Request.Files; if (httpRequest.Files.Count > 0) { foreach (string file in httpRequest.Files) { var postedFile = httpRequest.Files[file]; listFileName.Add(postedFile.FileName.Split('.')[0]); } } using (HttpClient client = new HttpClient()) { Dictionary<string, string> DicData = new Dictionary<string, string>(); DicData.Add("MethodName", methodsName); DicData.Add("ServerPath", serverPath); DicData.Add("ArtifactsNum", ArtifactsNum.ToString()); DicData.Add("SendTime", DateTime.Now.ToString()); DicData.Add("UserName", userName); DicData.Add("FileType", filesType.ToString()); DicData.Add("FileGuid", guidStr); client.MaxResponseContentBufferSize = 2147483647; client.Timeout = TimeSpan.FromMinutes(30); MediaTypeWithQualityHeaderValue temp = new MediaTypeWithQualityHeaderValue("application/json") { CharSet = "utf-8" }; client.DefaultRequestHeaders.Accept.Add(temp);//设定要响应的数据格式 using (var content = new MultipartFormDataContent())//表明是通过multipart/form-data的方式上传数据 { var formDatas = this.GetFormDataByteArrayContent(this.GetNameValueCollection(DicData));//获取键值集合对应 var files = this.GetFileByteArray(listContentByte, listFileName);//获取文件集合对应的ByteArrayContent集合 Action<List<ByteArrayContent>> act = (dataContents) => {//声明一个委托,该委托的作用就是将ByteArrayContent集合加入到MultipartFormDataContent中 foreach (var byteArrayContent in dataContents) { content.Add(byteArrayContent); } }; act(formDatas); act(files);//执行act try { url = string.Format(routeUrl + "api/Trading/Files/UploadOptimizeFile"); var returnResult = client.PostAsync(url, content).Result;//post请求 if (returnResult.StatusCode == HttpStatusCode.OK) result = returnResult.Content.ReadAsAsync<string>().Result; else result = "30|客户端连接路由端出错,错误代码:" + returnResult.StatusCode.ToString(); } catch (Exception ex) { result = "29|客户端连接超时"; } } } return Ok(result); } |
请发表评论