向远程服务器发送并接受文件
Posted 兴趣就是天赋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了向远程服务器发送并接受文件相关的知识,希望对你有一定的参考价值。
/// <summary> /// 发送文件 /// </summary> public static void PostBinaryData() { string notice = Convert.ToString(Guid.NewGuid()); string sign = MD5Helper.Encrypt("zoversoft" + notice); string url = string.Format("http://www.xx.com/File/SaveImageData?childFile={0}&sign={1}¬ice={2}", "lk", sign, notice); byte[] bytes = System.IO.File.ReadAllBytes(@"F:微信应用logo消息.jpg"); HttpWebRequest wRequest = (HttpWebRequest)WebRequest.Create(url); wRequest.ContentType = "multipart/form-data"; wRequest.ContentLength = bytes.Length; wRequest.Method = "POST"; using (Stream stream = wRequest.GetRequestStream()) { stream.Write(bytes, 0, bytes.Length); using (HttpWebResponse wResponse = (HttpWebResponse)wRequest.GetResponse()) { using (StreamReader sReader = new StreamReader(wResponse.GetResponseStream(), System.Text.Encoding.UTF8)) { Console.Write(sReader.ReadToEnd()); Console.ReadKey(); } } } }
/// <summary> /// 接收文件 /// </summary> /// <param name="childFile">文件夹</param> /// <param name="sign">签名</param> /// <param name="notice">随机字符串</param> /// <returns>结果/文件路径</returns> [HttpPost] public JsonResult SaveImageData(string childFile, string sign, string notice) { try { if (MD5Helper.Encrypt("zoversoft" + notice) == sign) { string filePath = IPublicVar.WebDownPath + childFile; if (System.IO.Directory.Exists(filePath) == false) { System.IO.Directory.CreateDirectory(filePath); } string fileName = string.Format("{0:yyyyMMdd}", DateTime.Now) + sign + ".jpg"; using (Bitmap img = new Bitmap(Request.InputStream)) { img.Save(filePath + @"" + fileName); return Json(childFile + "/" + fileName); } } else { return Json("0"); } } catch { return Json("-1"); } }
以上是关于向远程服务器发送并接受文件的主要内容,如果未能解决你的问题,请参考以下文章