|
1金钱
String fileToUpload = "f://testDown.gif";
String uploadUrl = "http://cloud.ibinfen.com/app/index.php";
//uploadUrl = "http://cloud.ibinfen.com/app/index.php?unit_id=2";
String fileFormName = "file";
String contenttype = "image/jpg";
string boundary = "----------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(uploadUrl);
webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
webrequest.Method = "post";
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(System.IO.Path.GetFileName(fileToUpload));
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);
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
FileStream fileStream = new FileStream(fileToUpload, FileMode.Open, FileAccess.Read);
long length = postHeaderBytes.Length+ fileStream.Length + boundaryBytes.Length;
webrequest.ContentLength = length;
Stream requestStream = webrequest.GetRequestStream();
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
MessageBox.Show(webrequest.Headers.ToString());
byte[] buffer = new Byte[(int)fileStream.Length];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
requestStream.Write(buffer, 0, bytesRead);
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
requestStream.Close();
WebResponse webRespon = webrequest.GetResponse();
Stream s = webRespon.GetResponseStream();
StreamReader sr = new StreamReader(s);
//读取服务器端返回的消息
String sReturnString = sr.ReadToEnd();
MessageBox.Show(sReturnString);
这是我上传图片的一个demo 我这边需要上传图片的时候 加个 参数 "unit_id=1",我应该把这个参数放到哪里呢 是头信息还是直接压到数据流里面? 试了好多次都不成功, 求大神帮助!!!!!!!!!!!!!!!!!!!!!!!!!!11
|
|