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

来源:互联网 发布:离子注入仿真软件 编辑:程序博客网 时间:2024/05/19 00:08

  用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";
         

        }




原创粉丝点击