加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

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

发布时间:2020-12-14 05:16:48 所属栏目:大数据 来源:网络整理
导读:/// 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/SaveIma
        /// <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,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");
            }
        }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读