向远程服务器发送并接受文件

        /// <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}&notice={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");
            }
        }

  

posted @ 2019-01-05 11:53  兴趣就是天赋  阅读(340)  评论(0编辑  收藏  举报