postman webapi webrequest 传送文件和接受文件

  用postman 提交文件 webapi作服务端接收数据 在webapi 内部用webrequest 向另一个webapi提交从postman接收的文件数据


 第一个webapi 接受代码


       [Route("postZData")]
        [HttpPost]
        public string postZData()
        {
            var file = HttpContext.Current.Request.Files[0];
            var filename = file.FileName;
            var fs =file.InputStream;
            byte[] filebyte = new byte[fs.Length];
            fs.Read(filebyte,0,Convert.ToInt32(fs.Length)+1);
            string url = "http://localhost:8010/webapitest/api/Values1/ReciveFileZip";
            CookieContainer cc = new CookieContainer();
            string resstr = UploadFileEx(filename,url,null,null,filebyte,cc);
            return resstr;
        }

public static string UploadFileEx(string filename, string url,
string fileFormName, string contenttype,byte[] filebyte, CookieContainer cookies)
        {
          
            if ((fileFormName == null) ||
            (fileFormName.Length == 0))
            {
                fileFormName = "file";
            }
            if ((contenttype == null) ||
            (contenttype.Length == 0))
            {
                contenttype = "application/octet-stream";
            }
          //  string postdata;
          
            Uri uri = new Uri(url);
          // string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
            HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uri);
            webrequest.CookieContainer = cookies;
            //webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
            webrequest.Method = "POST";
            // Build up the post message header 
            //StringBuilder sb = new StringBuilder();
            //sb.Append("--");
            //sb.Append(boundary);
            //sb.Append("\r\n");
            //sb.Append("Content-Disposition: form-data; name=\"");
            //sb.Append(fileFormName);
            //sb.Append("\"; filename=\"");
            //sb.Append(filename);
            //sb.Append("\"");
            //sb.Append("\r\n");
            //sb.Append("Content-Type: ");
            //sb.Append(contenttype);
            //sb.Append("\r\n");
            //sb.Append("\r\n");
            //string postHeader = sb.ToString();
            //byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
            // Build the trailing boundary string as a byte array 
            // ensuring the boundary appears on a line by itself 
            //  byte[] boundaryBytes =
            //Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");

            long length = filebyte.Length;// postHeaderBytes.Length + filebyte.Length + boundaryBytes.Length;
            webrequest.ContentLength = length;
            Stream requestStream = webrequest.GetRequestStream();
            // Write out our post header 
           // requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
            // Write out the file contents 
          //  byte[] buffer = new Byte[checked((uint)Math.Min(4096,
          //(int)fileStream.Length))];
            //int bytesRead = 0;
            //while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
                requestStream.Write(filebyte, 0, filebyte.Length);
            // Write out the trailing boundary 
          //  requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
            WebResponse responce = webrequest.GetResponse();
            Stream s = responce.GetResponseStream();
            StreamReader sr = new StreamReader(s);
            return sr.ReadToEnd();
        }

第二个webapi 接受方法

    [HttpPost]
        [Route("ReciveFileZip")]
        public string ReciveFileZip()
        {
            var file = HttpContext.Current.Request.InputStream;
            byte[] filebyte = new byte[file.Length];
               
            file.Read(filebyte,0,Convert.ToInt32(file.Length)+1);
            string filepath = @"E://1.txt";//如果是提交的是txt文件就是xx.txt 如果zip就是xx.zip 这两种我都试过了可以 至于其他格式请各位尝试
            File.WriteAllBytes(filepath,filebyte);
            return "chenggong";
         

        }




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值