public static bool SaveFile(string url, string fileName) { var result = false; try { using (var webClient = new WebClientUtil()) { webClient.DownloadFile(url, fileName); result = true; } } catch { } return(result); }
/// <summary> /// 上传数据到服务器 /// </summary> /// <param name="url"></param> /// <param name="data"></param> /// <param name="headers"></param> /// <returns></returns> public static byte[] Post(string url, byte[] data, WebHeaderCollection headers = null) { using (var client = new WebClientUtil()) { client.Encoding = Encoding.UTF8; client.Headers.Add(HttpRequestHeader.Accept, "*/*"); client.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)"); client.Headers.Add("Accept-Encoding", "gzip"); client.Headers.Add("ContentEncoding", "gzip"); if (headers != null) { foreach (var item in headers.AllKeys) { client.Headers.Add(item, headers[item]); } } return(client.UploadData(url, "POST", data)); } }
/// <summary> /// 上传到服务器 /// </summary> /// <param name="url"></param> /// <param name="image"></param> /// <returns></returns> public static string ToUrl(string url, Image image) { var result = string.Empty; try { var filePath = Environment.CurrentDirectory + "\\" + Guid.NewGuid().ToString("N") + ".png"; image.Save(filePath); using (var webClient = new WebClientUtil()) { result = Encoding.UTF8.GetString(webClient.UploadFile(url, filePath)); } image.Dispose(); GC.Collect(0); if (File.Exists(filePath)) { File.Delete(filePath); } } catch { } return(result); }