zoukankan      html  css  js  c++  java
  • 客户端通过http 传送多文件给服务端

     1  var path = @"C:\Users\HK\Desktop\项目1\00611844.tif";
     2             var path2 = @"C:\Users\HK\Desktop\项目1\006118441.tif";
     3             var uriString = @"http://localhost:5085/icapi/invoice/recognize/upload";
     4             var url = new Uri(uriString);
     5             // 设置参数
     6             HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
     7             CookieContainer cookieContainer = new CookieContainer();
     8             request.CookieContainer = cookieContainer;
     9             request.AllowAutoRedirect = true;
    10             request.Method = "POST";
    11             request.Timeout = 10000 * 30;
    12             string boundary = DateTime.Now.Ticks.ToString("X"); // 随机分隔线
    13             request.ContentType = "multipart/form-data;charset=utf-8;boundary=" + boundary;
    14             byte[] itemBoundaryBytes = Encoding.UTF8.GetBytes("
    --" + boundary + "
    ");
    15             byte[] endBoundaryBytes = Encoding.UTF8.GetBytes("
    --" + boundary + "--
    ");
    16 
    17             int pos = path.LastIndexOf("\");
    18             string fileName = path.Substring(pos + 1);
    19 
    20             int pos2 = path2.LastIndexOf("\");
    21             string fileName2 = path2.Substring(pos2 + 1);
    22 
    23             //请求头部信息 
    24             StringBuilder sbHeader = new StringBuilder(string.Format("Content-Disposition:form-data;name="file";filename="{0}"
    Content-Type:application/octet-stream
    
    ", fileName));
    25             byte[] postHeaderBytes = Encoding.UTF8.GetBytes(sbHeader.ToString());
    26 
    27             //请求头部信息2 
    28             StringBuilder sbHeader2 = new StringBuilder(string.Format("Content-Disposition:form-data;name="file";filename="{0}"
    Content-Type:application/octet-stream
    
    ", fileName2));
    29             byte[] postHeaderBytes2 = Encoding.UTF8.GetBytes(sbHeader2.ToString());
    30 
    31             FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);
    32             byte[] bArr = new byte[fs.Length];
    33             fs.Read(bArr, 0, bArr.Length);
    34             fs.Close();
    35 
    36             FileStream fs2 = new FileStream(path2, FileMode.Open, FileAccess.Read);
    37             byte[] bArr2 = new byte[fs2.Length];
    38             fs2.Read(bArr2, 0, bArr2.Length);
    39             fs2.Close();
    40 
    41             Stream postStream = request.GetRequestStream();
    42             postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
    43             postStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
    44             postStream.Write(bArr, 0, bArr.Length);
    45 
    46             postStream.Write(itemBoundaryBytes, 0, itemBoundaryBytes.Length);
    47             postStream.Write(postHeaderBytes2, 0, postHeaderBytes2.Length);
    48             postStream.Write(bArr2, 0, bArr2.Length);
    49             postStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length);
    50             postStream.Close();
    51 
    52             //发送请求并获取相应回应数据
    53             HttpWebResponse response = request.GetResponse() as HttpWebResponse;
    54             //直到request.GetResponse()程序才开始向目标网页发送Post请求
    55             Stream instream = response.GetResponseStream();
    56             StreamReader sr = new StreamReader(instream, Encoding.UTF8);
    57             //返回结果网页(html)代码
    58             string content = sr.ReadToEnd();

    上面的是客户端

    服务端:

    通过 

    Request.Files即可获得传过来的文件,一般是mvc比较支持文件的传输,但是在配置如果你想自己的方法加上特定的路由如:
    [RoutePrefix("icapi/invoice")]
        public class ArgRecognitionApiController : Controller
        {
            [Route("recognize/upload")]
            public JsonResult T()
            {
                JsonResult jsonResult = new JsonResult();
                List<BanthResult> result = new List<BanthResult>();
    
                jsonResult.Data = result;
    
                var fileCount = Request.Files.Count;
    
                for (int i = 0; i < fileCount; i++)
                {
                    var file = Request.Files[i];
                    BanthResult banthResult = new BanthResult();
                    Invoice inv = new Invoice();
                    banthResult.invoice = inv;
                    banthResult.fileId = file.FileName;
                    result.Add(banthResult);
                }
                return jsonResult;
            }
        }
    

      这个是后再配置路由的时候要加上

    routes.MapMvcAttributeRoutes();
    
  • 相关阅读:
    CODEVS 3137 栈练习1
    CODEVS 3138 栈练习2
    线段树———模板
    深度优先搜索与广度优先搜索———模板
    犯罪团伙 codevs 3554
    嘟!数字三角形 W WW WWW集合!
    寻找子串位置 codevs 1204
    流输入练习——寻找Sb.VI codevs 3096
    C++之路进阶——codevs3287(货车运输)
    c++之路进阶——codevs4543(普通平衡树)
  • 原文地址:https://www.cnblogs.com/HKKD/p/6378405.html
Copyright © 2011-2022 走看看