LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

c#使用HttpWebRequest上传文件同时携带其他参数

admin
2017年3月22日 0:30 本文热度 6174
 这个小程序参考了另一位博友的代码,做了稍许调整,创建的两个Handler程序,一个上传的ashx,一个接收的ashx

上传文件代码

[csharp] view plain copy
  1. public void ProcessRequest(HttpContext context)  
  2.         {  
  3.             //参考http://www.cnblogs.com/greenerycn/archive/2010/05/15/csharp_http_post.html  
  4.             string filePath = "d:\\apple4.jpg";  
  5.             string fileName = "apple4.jpg";  
  6.             string postURL = "http://192.168.1.11/testhandler/accfile.ashx";  
  7.               
  8.             // 边界符  
  9.             var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");  
  10.             var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");  
  11.             var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);  
  12.   
  13.             // 最后的结束符  
  14.             var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");  
  15.   
  16.             // 文件参数头  
  17.             const string filePartHeader =  
  18.                 "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +  
  19.                  "Content-Type: application/octet-stream\r\n\r\n";  
  20.             var fileHeader = string.Format(filePartHeader, "file", fileName);  
  21.             var fileHeaderBytes = Encoding.UTF8.GetBytes(fileHeader);  
  22.   
  23.             // 开始拼数据  
  24.             var memStream = new MemoryStream();  
  25.             memStream.Write(beginBoundary, 0, beginBoundary.Length);  
  26.   
  27.             // 文件数据  
  28.             memStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);  
  29.             var buffer = new byte[1024];  
  30.             int bytesRead; // =0  
  31.             while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)  
  32.             {  
  33.                 memStream.Write(buffer, 0, bytesRead);  
  34.             }  
  35.             fileStream.Close();  
  36.   
  37.             // Key-Value数据  
  38.             var stringKeyHeader = "\r\n--" + boundary +  
  39.                                    "\r\nContent-Disposition: form-data; name=\"{0}\"" +  
  40.                                    "\r\n\r\n{1}\r\n";  
  41.   
  42.             Dictionary<stringstring> stringDict = new Dictionary<stringstring>();  
  43.             stringDict.Add("len""500");  
  44.             stringDict.Add("wid""300");  
  45.             foreach (byte[] formitembytes in from string key in stringDict.Keys  
  46.                                              select string.Format(stringKeyHeader, key, stringDict[key])  
  47.                                                  into formitem  
  48.                                                  select Encoding.UTF8.GetBytes(formitem))  
  49.             {  
  50.                 memStream.Write(formitembytes, 0, formitembytes.Length);  
  51.             }  
  52.   
  53.             // 写入最后的结束边界符  
  54.             memStream.Write(endBoundary, 0, endBoundary.Length);  
  55.   
  56.             //倒腾到tempBuffer?  
  57.             memStream.Position = 0;  
  58.             var tempBuffer = new byte[memStream.Length];  
  59.             memStream.Read(tempBuffer, 0, tempBuffer.Length);  
  60.             memStream.Close();  
  61.   
  62.             // 创建webRequest并设置属性  
  63.             var webRequest = (HttpWebRequest)WebRequest.Create(postURL);  
  64.             webRequest.Method = "POST";  
  65.             webRequest.Timeout = 100000;  
  66.             webRequest.ContentType = "multipart/form-data; boundary=" + boundary;  
  67.             webRequest.ContentLength = tempBuffer.Length;  
  68.   
  69.             var requestStream = webRequest.GetRequestStream();  
  70.             requestStream.Write(tempBuffer, 0, tempBuffer.Length);  
  71.             requestStream.Close();  
  72.   
  73.             var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();  
  74.             string responseContent;  
  75.             using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(),Encoding.GetEncoding("utf-8")))  
  76.             {  
  77.                 responseContent = httpStreamReader.ReadToEnd();  
  78.             }  
  79.   
  80.             httpWebResponse.Close();  
  81.             webRequest.Abort();  
  82.   
  83.             context.Response.ContentType = "text/plain";  
  84.             context.Response.Write(responseContent);  
  85.         }  

接收文件

[csharp] view plain copy
  1. public void ProcessRequest(HttpContext context)  
  2.         {  
  3.             context.Response.ContentType = "text/plain";  
  4.             if (context.Request.Files.Count == 0)  
  5.             {  
  6.                 context.Response.Write("No file");  
  7.                 return;  
  8.             }  
  9.   
  10.             HttpPostedFile f1 = context.Request.Files[0];  
  11.             System.Drawing.Image image = System.Drawing.Image.FromStream(f1.InputStream);  
  12.             image.Save("d:\\upload.jpg");  
  13.               
  14.             string strPars="";  
  15.             foreach (var key in context.Request.Form.AllKeys)  
  16.             {  
  17.                 string val = context.Request[key];  
  18.                 strPars += "["+key + ":" + val + "] ";  
  19.             }  
  20.   
  21.             image.Dispose();  
  22.             context.Response.Write("OK Get File:" + f1.FileName + " Pars:" + strPars);  
  23.         }  

该文章在 2017/3/22 0:30:22 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved