• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

postman上线文件上传,并用c#服务端接收

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

一:用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);
        }

鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap