后台下载微信js sdk上传的图片

来源:互联网 发布:珠海长隆 知乎 编辑:程序博客网 时间:2024/05/25 19:58
public static string DownloadWxImg(string token, string media)
        {
            var url = string.Format("https://api.weixin.qq.com/cgi-bin/media/get?access_token={0}&media_id={1}", token, media);
            string path = "";
            string fileName = "";
            HttpWebRequest request = null;
            HttpWebResponse response = null;


            //请求url以获取数据
            try
            {
                DateTime dt = DateTime.Now;
                path = string.Format("/ImageStore/{0}/{1}{2}", dt.Year, dt.Month.ToString().PadLeft(2, '0'), dt.Day.ToString().PadLeft(2, '0'));
                fileName = System.Guid.NewGuid().ToString() + ".jpg";
                string abtPath = HttpContext.Current.Server.MapPath(path);
                if (!CreateDirectory(abtPath))
                {
                    Log4NetHelper.Log.Error(string.Format("创建目录{0}失败", abtPath));
                    return "";
                }
                //设置最大连接数
                ServicePointManager.DefaultConnectionLimit = 200;
                //设置https验证方式
                if (url.StartsWith("https", StringComparison.OrdinalIgnoreCase))
                {
                    ServicePointManager.ServerCertificateValidationCallback =
                            new RemoteCertificateValidationCallback(CheckValidationResult);
                }


                /***************************************************************
                * 下面设置HttpWebRequest的相关属性
                * ************************************************************/
                request = (HttpWebRequest)WebRequest.Create(url);


                request.Method = "GET";


                //设置代理
                //WebProxy proxy = new WebProxy();
                //proxy.Address = new Uri(WxPayConfig.PROXY_URL);
                //request.Proxy = proxy;


                //获取服务器返回
                response = (HttpWebResponse)request.GetResponse();


                //获取HTTP返回数据


                BinaryReader br = new BinaryReader(response.GetResponseStream());
                FileStream fs = new FileStream(abtPath + "\\" + fileName, FileMode.Create, FileAccess.Write);


                const int buffsize = 1024;
                byte[] bytes = new byte[buffsize];
                int totalread = 0;


                int numread = buffsize;
                while (numread != 0)
                {
                    // read from source  
                    numread = br.Read(bytes, 0, buffsize);
                    totalread += numread;


                    // write to disk  
                    fs.Write(bytes, 0, numread);
                }


                br.Close();
                fs.Close();


            }
            catch (Exception e)
            {
                Log4NetHelper.Log.Error(string.Format("【DownloadWxImg】意外错误:{0}", e.Message));
                throw new Exception(e.Message);
            }
            finally
            {
                //关闭连接和流
                if (response != null)
                {
                    response.Close();
                }
                if (request != null)
                {
                    request.Abort();
                }
            }
            if (!string.IsNullOrEmpty(path))
            {
                path = string.Format("{0}/{1}", path, fileName);
            }
            return path;
        }
0 0
原创粉丝点击