public ContentResult UpFileTest()
{
try
{
string basePath = “/UploadFile/WXSmall/”;
basePath = System.Web.HttpContext.Current.Server.MapPath(basePath);
if (!System.IO.Directory.Exists(basePath))
{
System.IO.Directory.CreateDirectory(basePath);
}
HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
if (files != null)
{
for (int i = 0; i < files.Count; i++)
{
string ex = Path.GetExtension(files[i].FileName);
string fileName = Guid.NewGuid().ToString(“N”) + ex;
string filePath = basePath + fileName;
var file = Request.Files[i];
byte[] uploadFileBytes = new byte[files.Get(i).ContentLength];
file.InputStream.Read(uploadFileBytes, 0, file.ContentLength);
#region 通过微信API验证图片合法性
string exUp = ex.ToUpper();
if (exUp.Contains(“PNG”) || exUp.Contains(“JPEG”) || exUp.Contains(“JPG”) || exUp.Contains(“GIF”))
{
//从配置文件中获取AppId和AppSecret
var AppId = System.Configuration.ConfigurationManager.AppSettings[“SMAppId”].ToString();
var AppSecret = System.Configuration.ConfigurationManager.AppSettings[“SMAppSecret”].ToString();
//获取Token
var myToken = WXUtils.GetAccessToken(AppId, AppSecret);
//验证
var result = WXUtils.ImgSecCheck(myToken, uploadFileBytes);
if (!result)
{
return Content(“0”);
}
}
#endregion
}
}
return Content(“1”);
}
catch (Exception ex)
{
return Content(“0”);
}
}
/// <summary>
/// 图片验证
/// </summary>
/// <param name=“myToken”>令牌</param>
/// <param name=“bytes”>上传文件二进制流</param>
/// <returns></returns>
public static bool ImgSecCheck(string myToken, byte[] bytes)
{
var result = true;
string conUrl = “https://api.weixin.qq.com/wxa/img_sec_check?access_token=” + myToken;
var conResult = CommonMethod.FilePost(conUrl, bytes);
//Log.Info(“WXUtils”, “图片验证返回结果:”+conResult+",时间:"+DateTime.Now.ToString(“yyyy-MM-dd HH:mm:ss”));
JObject joResult = (JObject)JsonConvert.DeserializeObject(conResult);
if (joResult[“errcode”].ToString() != “0”)
{
return false;
}
return result;
}
/// <summary>
/// Post/get 提交调用抓取
/// </summary>
/// <param name=“url”>提交地址</param>
/// <param name=“param”>参数</param>
/// <returns>string</returns>
public static string FilePost(string sUrl,byte[] bytes)
{
Uri uriurl = new Uri(sUrl);
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uriurl);//HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url + (url.IndexOf("?") > -1 ? “” : “?”) + param);
req.Method = “Post”;
req.Timeout = 120 * 1000;
req.ContentType = “application/octet-stream”;
req.ContentLength = bytes.Length;
using (Stream reqStream = req.GetRequestStream())//using 使用可以释放using段内的内存
{
reqStream.Write(bytes, 0, bytes.Length);
reqStream.Flush();
}
try
{
using (WebResponse res = req.GetResponse())
{
//在这里对接收到的页面内容进行处理
Stream resStream = res.GetResponseStream();
StreamReader resStreamReader = new StreamReader(resStream, System.Text.Encoding.UTF8);
string resLine;
System.Text.StringBuilder resStringBuilder = new System.Text.StringBuilder();
while ((resLine = resStreamReader.ReadLine()) != null)
{
resStringBuilder.Append(resLine + System.Environment.NewLine);
}
resStream.Close();
resStreamReader.Close();
return resStringBuilder.ToString();
}
}
catch (Exception ex)
{
return ex.Message;//url错误时候回报错
}
}