所以只要拼一个这样的byte[] data数据Post过去,就可以达到同样的效果了。但是一定要注意,对于这种带有文件上传的,其ContentType是不一样的,例如上面的这种,其ContentType为"multipart/form-data; boundary=---------------------------7d429871607fe"。有了ContentType,我们就可以知道boundary(就是上面的"---------------------------7d429871607fe"),知道boundary了我们就可以构造出我们所需要的byte[] data了,最后,不要忘记,把我们构造的ContentType传到WebClient中(例如:webClient.Headers.Add("Content-Type", ContentType);)这样,就可以通过WebClient.UploadData 方法上载文件数据了。 具体代码如下: 生成二进制数据类的封装 using System; using System.Web; using System.IO; using System.Net; using System.Text; using System.Collections; namespace UploadData.Common ...{ /**//// /// 创建WebClient.UploadData方法所需二进制数组 /// public class CreateBytes ...{ Encoding encoding = Encoding.UTF8; /**//// /// 拼接所有的二进制数组为一个数组 /// /// 数组 /// /// 加上结束边界 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; }
页次:2/5 1篇/页 首页 上一页 下一页 尾页 合计 5 篇 Go: |