用于客户端上传文件和提交数据
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
namespace Net.Http
{
public class UploadTools
{
public static void Post(string url, string filename)
{
Post(url, filename, System.IO.File.ReadAllBytes(filename), null);
}
public static void Post(string url, string filename, Dictionary<string, string> paramsvalue)
{
Post(url, filename, System.IO.File.ReadAllBytes(filename), paramsvalue);
}
public static void Post(string url, Dictionary<string, string> paramsvalue)
{
Post(url, string.Empty, null, paramsvalue);
}
public static void Post(string url, string key, string value)
{
Dictionary<string, string> paramsvalue = new Dictionary<string, string>();
paramsvalue.Add(key, value);
Post(url, string.Empty, null, paramsvalue);
}
public static void Post(string url, string filename, byte[] filedata, Dictionary<string, string> paramsvalue)
{
string boundary = "Upload_" + DateTime.Now.ToString("yyyyMMddHHmmssfff");
string Enter = "\r\n";
string itemfile = "--" + boundary + Enter
+ "Content-Type:application/octet-stream" + Enter
+ "Content-Disposition: form-data; name=\"fileContent\"; filename=\"" + filename + "\"" + Enter + Enter;
StringBuilder sb = new StringBuilder();
foreach (var key in paramsvalue)
{
string item = Enter + "--" + boundary + Enter
+ "Content-Disposition: form-data; name=\"" + key.Key + "\"" + Enter + Enter
+ key.Value + Enter;
sb.Append(item);
}
string keyvalue = sb.ToString();
string endstr= "--" + boundary + "--";
byte[] itemfiledata = Encoding.UTF8.GetBytes(itemfile);
byte[] itemdata = Encoding.UTF8.GetBytes(keyvalue);
var endata = Encoding.UTF8.GetBytes(endstr);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "POST";
request.ContentType = "multipart/form-data;boundary=" + boundary;
Stream requeststream = request.GetRequestStream();
if (paramsvalue.Count < 1)
{
requeststream.Write(itemfiledata, 0, itemfiledata.Length);
requeststream.Write(endata, 0, endata.Length);
requeststream.Write(filedata, 0, filedata.Length);
}
else
{
if (!string.IsNullOrWhiteSpace(filename))
{
requeststream.Write(itemfiledata, 0, itemfiledata.Length);
requeststream.Write(filedata, 0, filedata.Length);
}
requeststream.Write(itemdata, 0, itemdata.Length);
requeststream.Write(endata, 0, endata.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream responsestream = response.GetResponseStream();
StreamReader myStreamReader = new StreamReader(responsestream, Encoding.GetEncoding("utf-8"));
string result = myStreamReader.ReadToEnd();
myStreamReader.Close();
responsestream.Close();
}
}
}
c# ASP.NET WEBFORM 服务器端
public partial class UpLoadFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string path = ConfigTools.GetKey("UploadSavePath", string.Empty);
if (string.IsNullOrEmpty(path))
{
path = MapPath("\\File\\");
}
string name = Request["updateTime"];
if (Request.Files.Count > 0)
{
HttpPostedFile file = Request.Files[0];
string filename = path + DateTime.Now.ToString("yyyyMMddHHmmss") + System.IO.Path.GetFileName(file.FileName);
file.SaveAs(filename);
}
Response.Write("Success\r\n");
}
}
测试代码:
private void button6_Click(object sender, EventArgs e)
{
Dictionary<string, string> dics = new Dictionary<string, string>();
dics.Add("updateTime", "aaaaa");
OpenFileDialog dlg = new OpenFileDialog();
dlg.ShowDialog();
UploadTools.Post("http://localhost:8888/testproject/Pages/UpLoadFile.aspx", dlg.FileName, dics);
}
private void button7_Click(object sender, EventArgs e)
{
Dictionary<string, string> dics = new Dictionary<string, string>();
dics.Add("updateTime", "aaaaa");
UploadTools.Post("http://localhost:8888/testproject/Pages/UpLoadFile.aspx", dics);
}