后台下载微信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;
}
{
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
- 后台下载微信js sdk上传的图片
- 微信js-sdk 上传图片、下载图片接口
- 微信js-sdk,选择图片,上传,下载到本地,php服务端
- 微信js sdk图片接口 上传图片
- 调用微信JS-SDK接口上传图片
- 微信js-sdk上传多个图片
- NET/C# 微信企业号 JS-SDK:上传图片
- //兼容苹果手机的SDK微信上传图片 下载到服务器
- 微信JS-SDK选取手机照片上传并下载保存至自己的服务器
- 微信下载用户上传的图片
- 微信js上传的图片只能保存3天 下载到自己的服务器
- 微信JS—SDK嵌套选择图片和上传图片接口,实现一键上传图片,遇到问题
- 利用微信js-sdk上传图片,在安卓下无反应,在ios下报miss miss argument的错
- 微信js sdk 授权上传头像下载代码片段和注意事项
- 微信js-sdk图片接口实现一键上传.md
- 微信JS-SDK加载验证流程和手机图片上传
- 调用微信JS-SDK上传图片爬坑之路
- 微信js-sdk 图片接口
- OKHttp请求网络数据
- UILabel文字大小自适应之--SizeToFit
- Android ActionBar应用实战,高仿微信主界面的设计
- flex布局
- AFNetWorking一般用法(一)
- 后台下载微信js sdk上传的图片
- 详解强大的jQuery选择器之过滤选择器、表单选择器
- HBase Admin createTable 创建表
- Material Design 最全解析_3 主题Theme
- 怎样通过ZBrush中的DynaMesh创建身体(二)
- iOS图文混排的简单实现
- sqlserver中修改自增列
- 修改PHP文件上传大小限制
- LeetCode #21 Merge Two Sorted Lists C# Solution