c#使用HttpWebRequest上传文件同时携带其他参数
|
admin
2017年3月22日 0:30
本文热度 6344
|
这个小程序参考了另一位博友的代码,做了稍许调整,创建的两个Handler程序,一个上传的ashx,一个接收的ashx
上传文件代码
- public void ProcessRequest(HttpContext context)
- {
-
- string filePath = "d:\\apple4.jpg";
- string fileName = "apple4.jpg";
- string postURL = "http://192.168.1.11/testhandler/accfile.ashx";
-
-
- var boundary = "---------------" + DateTime.Now.Ticks.ToString("x");
- var beginBoundary = Encoding.ASCII.GetBytes("--" + boundary + "\r\n");
- var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);
-
-
- var endBoundary = Encoding.ASCII.GetBytes("--" + boundary + "--\r\n");
-
-
- const string filePartHeader =
- "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" +
- "Content-Type: application/octet-stream\r\n\r\n";
- var fileHeader = string.Format(filePartHeader, "file", fileName);
- var fileHeaderBytes = Encoding.UTF8.GetBytes(fileHeader);
-
-
- var memStream = new MemoryStream();
- memStream.Write(beginBoundary, 0, beginBoundary.Length);
-
-
- memStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length);
- var buffer = new byte[1024];
- int bytesRead;
- while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
- {
- memStream.Write(buffer, 0, bytesRead);
- }
- fileStream.Close();
-
-
- var stringKeyHeader = "\r\n--" + boundary +
- "\r\nContent-Disposition: form-data; name=\"{0}\"" +
- "\r\n\r\n{1}\r\n";
-
- Dictionary<string, string> stringDict = new Dictionary<string, string>();
- stringDict.Add("len", "500");
- stringDict.Add("wid", "300");
- foreach (byte[] formitembytes in from string key in stringDict.Keys
- select string.Format(stringKeyHeader, key, stringDict[key])
- into formitem
- select Encoding.UTF8.GetBytes(formitem))
- {
- memStream.Write(formitembytes, 0, formitembytes.Length);
- }
-
-
- memStream.Write(endBoundary, 0, endBoundary.Length);
-
-
- memStream.Position = 0;
- var tempBuffer = new byte[memStream.Length];
- memStream.Read(tempBuffer, 0, tempBuffer.Length);
- memStream.Close();
-
-
- var webRequest = (HttpWebRequest)WebRequest.Create(postURL);
- webRequest.Method = "POST";
- webRequest.Timeout = 100000;
- webRequest.ContentType = "multipart/form-data; boundary=" + boundary;
- webRequest.ContentLength = tempBuffer.Length;
-
- var requestStream = webRequest.GetRequestStream();
- requestStream.Write(tempBuffer, 0, tempBuffer.Length);
- requestStream.Close();
-
- var httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
- string responseContent;
- using (var httpStreamReader = new StreamReader(httpWebResponse.GetResponseStream(),Encoding.GetEncoding("utf-8")))
- {
- responseContent = httpStreamReader.ReadToEnd();
- }
-
- httpWebResponse.Close();
- webRequest.Abort();
-
- context.Response.ContentType = "text/plain";
- context.Response.Write(responseContent);
- }
接收文件- public void ProcessRequest(HttpContext context)
- {
- context.Response.ContentType = "text/plain";
- if (context.Request.Files.Count == 0)
- {
- context.Response.Write("No file");
- return;
- }
-
- HttpPostedFile f1 = context.Request.Files[0];
- System.Drawing.Image image = System.Drawing.Image.FromStream(f1.InputStream);
- image.Save("d:\\upload.jpg");
-
- string strPars="";
- foreach (var key in context.Request.Form.AllKeys)
- {
- string val = context.Request[key];
- strPars += "["+key + ":" + val + "] ";
- }
-
- image.Dispose();
- context.Response.Write("OK Get File:" + f1.FileName + " Pars:" + strPars);
- }
该文章在 2017/3/22 0:30:22 编辑过