/// <summary> /// 文件发送 /// </summary> /// <param name="login"></param> /// <param name="listViewItem"></param> /// <param name="FilePath"></param> /// <param name="FileName"></param> public static void FileUpload(Login login, ListViewItem listViewItem, string FilePath, string FileName) { try { using (FileStream fileStream = File.OpenRead(FilePath)) { // 设置请求参数 Dictionary <string, string> PostData = new Dictionary <string, string>(); PostData.Add("task", "1"); PostData.Add("folder_id", "-1"); PostData.Add("id", "WU_FILE_0"); PostData.Add("name", FilePath); PostData.Add("type", GetType(Path.GetExtension(FilePath))); PostData.Add("lastModifiedDate", ToGMTFormat(DateTime.Now)); PostData.Add("size", fileStream.Length.ToString()); UploadType uploadType = new UploadType { Login = login, ViewItem = listViewItem, FilePath = FilePath, FileNameValue = FileName, FileTypeExtension = GetType(Path.GetExtension(FilePath)), UploadStream = fileStream, PostData = PostData }; // 执行上传 NetWork.UploadFile(uploadType, "https://up.woozooo.com/fileup.php"); } } catch (Exception ex) { Console.WriteLine("上传错信息: " + ex.Message); } }
public static void UploadFile(UploadType uploadType, string Url) { using (MemoryStream memoryStream = new MemoryStream()) { // 定义分节符 // 分界线可以自定义参数 string boundary = string.Format("----{0}", DateTime.Now.Ticks.ToString("x")), beginBoundary = string.Format("--{0}\r\n", boundary), endBoundary = string.Format("\r\n--{0}--\r\n", boundary); // 将字符串编码为字节序列 byte[] beginBoundaryBytes = Encoding.UTF8.GetBytes(beginBoundary), endBoundaryBytes = Encoding.UTF8.GetBytes(endBoundary); // 组装开始分界线数据体 到内存流中 memoryStream.Write(beginBoundaryBytes, 0, beginBoundaryBytes.Length); // 请求参数写入数据流 foreach (KeyValuePair <string, string> keyValuePair in uploadType.PostData) { string parameterHeaderTemplate = string.Format("Content-Disposition: form-data; name=\"{0}\"\r\n\r\n{1}\r\n{2}", keyValuePair.Key, keyValuePair.Value, beginBoundary); byte[] parameterHeaderBytes = Encoding.UTF8.GetBytes(parameterHeaderTemplate); memoryStream.Write(parameterHeaderBytes, 0, parameterHeaderBytes.Length); } // 文件类型名称 string fileHeaderTemplate = string.Format("Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n", uploadType.FileNameKey, uploadType.FileNameValue, uploadType.FileTypeExtension); byte[] fileHeaderBytes = Encoding.UTF8.GetBytes(fileHeaderTemplate); memoryStream.Write(fileHeaderBytes, 0, fileHeaderBytes.Length); // 组装文件流 到内存流中 byte[] buffer = new byte[1024 * 1024 * 1]; int size = uploadType.UploadStream.Read(buffer, 0, buffer.Length); while (size > 0) { memoryStream.Write(buffer, 0, size); size = uploadType.UploadStream.Read(buffer, 0, buffer.Length); } // 组装结束分界线数据体 到内存流中 memoryStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); // 获取二进制数据 byte[] postBytes = memoryStream.ToArray(); // HttpWebRequest 组装 HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(new Uri(Url, UriKind.RelativeOrAbsolute)); webRequest.Method = "POST"; webRequest.Timeout = 20000; webRequest.ServicePoint.Expect100Continue = false; HttpRequestCachePolicy noCachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore); webRequest.CachePolicy = noCachePolicy; ServicePoint currentServicePoint = webRequest.ServicePoint; currentServicePoint.ConnectionLimit = 65500; // 写入 ContentType webRequest.ContentType = string.Format("multipart/form-data; boundary={0}", boundary); webRequest.KeepAlive = false; // 写入cookie webRequest.CookieContainer = uploadType.Login.GetCookieContainer(); // 判断是否HTTPS请求 if (Regex.IsMatch(Url, "^https://")) { ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls; ServicePointManager.ServerCertificateValidationCallback = CheckValidationResult; } // 9.写入上传请求数据 using (Stream requestStream = webRequest.GetRequestStream()) { requestStream.Write(postBytes, 0, postBytes.Length); requestStream.Close(); } // 10.获取响应 using (HttpWebResponse webResponse = (HttpWebResponse)webRequest.GetResponse()) { using (StreamReader reader = new StreamReader(webResponse.GetResponseStream(), Encoding.UTF8)) { string body = reader.ReadToEnd(); ReturnJson ReturnJson = JsonConvert.DeserializeObject <ReturnJson>(body); uploadType.ViewItem.SubItems[3].Text = ReturnJson.zt == 1 ? "√" : "X"; uploadType.ViewItem.SubItems[4].Text = ReturnJson.info; reader.Close(); } } } }