標籤:
public class CreateBytes { Encoding encoding = Encoding.UTF8; /**/ /// <summary> /// 拼接所有的位元組為一個數組 /// </summary> /// <param name="byteArrays">數組</param> /// <returns></returns> /// <remarks>加上結束邊界</remarks> public byte[] JoinBytes(ArrayList byteArrays) { int length = 0; int readLength = 0; // 加上結束邊界 string endBoundary = Boundary + "--\r\n"; //結束邊界 byte[] endBoundaryBytes = encoding.GetBytes(endBoundary); byteArrays.Add(endBoundaryBytes); foreach (byte[] b in byteArrays) { length += b.Length; } byte[] bytes = new byte[length]; // 遍曆複製 // foreach (byte[] b in byteArrays) { b.CopyTo(bytes, readLength); readLength += b.Length; } return bytes; } public bool UploadData(string uploadUrl, byte[] bytes, out byte[] responseBytes) { WebClient webClient = new WebClient(); webClient.Headers.Add("Content-Type", ContentType); try { responseBytes = webClient.UploadData(uploadUrl, bytes); return true; } catch (WebException ex) { Stream resp = ex.Response.GetResponseStream(); responseBytes = new byte[ex.Response.ContentLength]; resp.Read(responseBytes, 0, responseBytes.Length); } return false; } /**/ /// <summary> /// 擷取普通表單區域位元組 /// </summary> /// <param name="fieldName">表單名</param> /// <param name="fieldValue">表單值</param> /// <returns></returns> /// <remarks> /// -----------------------------7d52ee27210a3c\r\nContent-Disposition: form-data; name=\"表單名\"\r\n\r\n表單值\r\n /// </remarks> public byte[] CreateFieldData(string fieldName, string fieldValue) { string textTemplate = Boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n"; string text = String.Format(textTemplate, fieldName, fieldValue); byte[] bytes = encoding.GetBytes(text); return bytes; } /**/ /// <summary> /// 擷取檔案上傳表單區域位元組 /// </summary> /// <param name="fieldName">表單名</param> /// <param name="filename">檔案名稱</param> /// <param name="contentType">檔案類型</param> /// <param name="contentLength">檔案長度</param> /// <param name="stream">檔案流</param> /// <returns>位元組</returns> public byte[] CreateFieldData(string fieldName, string filename, string contentType, byte[] fileBytes) { string end = "\r\n"; string textTemplate = Boundary + "\r\nContent-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n"; // 頭資料 string data = String.Format(textTemplate, fieldName, filename, contentType); byte[] bytes = encoding.GetBytes(data); // 尾資料 byte[] endBytes = encoding.GetBytes(end); // 合成後的數組 byte[] fieldData = new byte[bytes.Length + fileBytes.Length + endBytes.Length]; bytes.CopyTo(fieldData, 0); // 頭資料 fileBytes.CopyTo(fieldData, bytes.Length); // 檔案的位元據 endBytes.CopyTo(fieldData, bytes.Length + fileBytes.Length); // \r\n return fieldData; } #region 屬性 public string Boundary { get { string[] bArray, ctArray; string contentType = ContentType; ctArray = contentType.Split(‘;‘); if (ctArray[0].Trim().ToLower() == "multipart/form-data") { bArray = ctArray[1].Split(‘=‘); return "--" + bArray[1]; } return null; } } public string ContentType { get { if (HttpContext.Current == null) { return "multipart/form-data; boundary=---------------------------7d5b915500cee"; } return HttpContext.Current.Request.ContentType; } } #endregion }
調用方式如下:
CreateBytes cb = new CreateBytes(); // 所有表單資料 ArrayList bytesArray = new ArrayList(); // 普通表單 bytesArray.Add(cb.CreateFieldData("jsonDataStr", "")); // 讀檔案流 FileStream fs = new FileStream("wolfy.JPG", FileMode.Open, FileAccess.Read, FileShare.Read); string ContentType = "application/octet-stream"; byte[] fileBytes = new byte[fs.Length]; fs.Read(fileBytes, 0, Convert.ToInt32(fs.Length)); // 檔案表單 bytesArray.Add(cb.CreateFieldData("file1", "wolfy.JPG", ContentType, fileBytes)); bytesArray.Add(cb.CreateFieldData("file2", "wolfy1.JPG", ContentType, fileBytes)); // 合成所有表單並產生位元組 byte[] bytes = cb.JoinBytes(bytesArray); // 返回的內容 byte[] responseBytes; bool uploaded = cb.UploadData("http://localhost:8700/api/System/ImgUpload", bytes, out responseBytes);
WebClient 實現多檔案/文本同時上傳