public ActionResult UploadImge(string serverId)
{
var headPath = "/UploadImage/" + DateTime.Now.ToString("yyyyMM");
string AbsoluteFilePath = Server.MapPath(headPath);
if (!Directory.Exists(AbsoluteFilePath))
{
Directory.CreateDirectory(AbsoluteFilePath);
}
string imgPath = headPath + "/" + DateTime.Now.ToString("yyyyMMddHHmm") + serverId + ".jpg";//原图
string AbsolutePath = Server.MapPath(imgPath);
WeChatClient.SaveMultimedia(serverId, AbsolutePath); return Json(imgPath); } public ActionResult UploadImge2(string serverId)
{
var headPath = "/UploadImage/" + DateTime.Now.ToString("yyyyMM");
string AbsoluteFilePath = Server.MapPath(headPath);
if (!Directory.Exists(AbsoluteFilePath))
{
Directory.CreateDirectory(AbsoluteFilePath);
}
string imgPath = headPath + "/" + DateTime.Now.ToString("yyyyMMddHHmm") + serverId + ".jpg";
string AbsolutePath = Server.MapPath(imgPath); WeChatClient.SaveMultimedia(serverId, AbsolutePath); CommonUtil.MakeThumbnail(AbsolutePath, AbsolutePath.Replace(".jpg", "_2.jpg"),, );//以3:4比例裁剪
return Json(imgPath); }
public ActionResult UploadImge3(string serverId)
{
var headPath = "/UploadImage/" + DateTime.Now.ToString("yyyyMM");
string AbsoluteFilePath = Server.MapPath(headPath);
if (!Directory.Exists(AbsoluteFilePath))
{
Directory.CreateDirectory(AbsoluteFilePath);
}
string imgPath = headPath + "/" + DateTime.Now.ToString("yyyyMMddHHmm") + serverId + ".jpg";
string AbsolutePath = Server.MapPath(imgPath); WeChatClient.SaveMultimedia(serverId, AbsolutePath); CommonUtil.MakeThumbnail(AbsolutePath, AbsolutePath.Replace(".jpg", "_3.jpg"), , );//以4:3比例裁剪图片
return Json(imgPath); }
  public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int bw = , int bh = )
{
try
{
Image originalImage = Image.FromFile(originalImagePath);
var bl = originalImage.Height * 1.00 / originalImage.Width;
int orgWith, orgHeight, startY = , startX = , newWith, newHeight = ;
var bd = bh * 1.00 / bw;
if (bl > bd)
{
orgWith = originalImage.Width;
orgHeight = (int)(originalImage.Width * bh / bw);
startY = (originalImage.Height - orgHeight) / ;
newWith = orgWith;
if (newWith > )
{
newWith = ;
}
newHeight = (int)(newWith * bh / bw); }
else
{
orgWith = originalImage.Height * bw / bh;
orgHeight = originalImage.Height;
startX = (originalImage.Width - orgWith) / ;
newWith = orgWith;
if (newWith > )
{
newWith = ;
}
newHeight = (int)(newWith * bh / bw);
} Bitmap destBitmap = new Bitmap(newWith, newHeight);//目标图
Rectangle destRect = new Rectangle(, , newWith, newHeight);//矩形容器
Rectangle srcRect = new Rectangle(startX, startY, orgWith, orgHeight); var g = Graphics.FromImage(destBitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(Color.Transparent); try
{
g.DrawImage(originalImage, destRect, srcRect, GraphicsUnit.Pixel);
destBitmap.Save(thumbnailPath, originalImage.RawFormat);
}
catch (System.Exception e)
{ }
finally
{
originalImage.Dispose();
destBitmap.Dispose();
g.Dispose();
} }
catch (Exception)
{ }
} }
public static void SaveMultimedia(string MEDIA_ID, string savepath)
{
string file = string.Empty;
string strpath = string.Empty;
string stUrl = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" + MyToken.Token + "&media_id=" + MEDIA_ID;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(stUrl);
req.Method = "GET";
using (WebResponse wr = req.GetResponse())
{
HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();
strpath = myResponse.ResponseUri.ToString();
WebClient mywebclient = new WebClient();
try
{
mywebclient.DownloadFile(strpath, savepath);
}
catch (Exception ex)
{
savepath = ex.ToString();
} } }
 var uploadNum = 0;
wx.config({
debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
appId: '********', // 必填,公众号的唯一标识
timestamp: '@(ViewBag.timestamp)', // 必填,生成签名的时间戳
nonceStr: '@(ViewBag.nonceStr)', // 必填,生成签名的随机串
signature: '@(ViewBag.signature)',// 必填,签名,见附录1
jsApiList: ['chooseImage', 'previewImage', 'uploadImage', 'downloadImage'] // 必填,需要使用的JS接口列表,所有JS接口列表见附录2
}); function uploadImge() {
var ttnum = 5 - uploadNum;
wx.chooseImage({
count: ttnum, // 默认9
sizeType: ['compressed'], // 可以指定是原图还是压缩图,默认二者都有
sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
success: function (res) {
var localIds = res.localIds; // 返回选定照片的本地ID列表,localId可以作为img标签的src属性显示图片
uploadImg(localIds, 0);
}
});
} function uploadImg(localIds, num) {
if (localIds.length <= num) {
return;
}
var localId = localIds[num]; wx.uploadImage({
localId: localId, // 需要上传的图片的本地ID,由chooseImage接口获得
isShowProgressTips: 1, // 默认为1,显示进度提示
success: function (sres) {
var serverId = sres.serverId; // 返回图片的服务器端ID
$.post("/WeiXin/Home/UploadImge3", { serverId: serverId }, function (data) {
var picPath = $("#PicPath").val();
if (picPath == "") {
picPath = data;
} else {
picPath = picPath + "," + data;
}
$("#PicPath").val(picPath); var temphtml = ""; if (window.__wxjs_is_wkwebview) {
wx.getLocalImgData({
localId: localId, // 图片的localID
success: function (res) {
var localData = res.localData; // localData是图片的base64数据,可以用img标签显示
temphtml = '<div class="col-xs-6"><img src="/Content/img/叉-m.png" style="width:20px;height:20px;position:absolute;z-index:2;right:15px;" onclick="delimg(this,\'' + data + '\')"/><img src="' + localData + '" class="picimg" style="z-index:1;position:absolute"/>'; $("#picdiv").before(temphtml);
uploadNum = uploadNum + 1;
if (uploadNum >= 5) {
$("#picdiv").hide();
}
uploadImg(localIds, num + 1)
}
});
} else {
temphtml = '<div class="col-xs-6"><img src="/Content/img/叉-m.png" style="width:20px;height:20px;position:absolute;z-index:2;right:15px;" onclick="delimg(this,\'' + data + '\')"/><img src="' + localId + '" class="picimg" style="z-index:1;position:absolute"/>';
$("#picdiv").before(temphtml);
uploadNum = uploadNum + 1;
if (uploadNum >= 5) {
$("#picdiv").hide();
}
uploadImg(localIds, num + 1)
}
}) }
}); }

调用微信JS上传照片接口上传图片的更多相关文章

  1. mui调用本地相册调用相机上传照片

    调用mui的常用库和jquery html部分: <header class="mui-bar mui-bar-nav"> <a class="mui- ...

  2. PHP -- 上传文件接口编写 及 iOS -- 端上传图片AF实现

    PHP 上传文件接口: //保存图片 $json_result ['status'] = 0; $path = 'upfile'; $json_result ['status'] = 0; $json ...

  3. Android仿微信图片上传,可以选择多张图片,缩放预览,拍照上传等

    仿照微信,朋友圈分享图片功能 .可以进行图片的多张选择,拍照添加图片,以及进行图片的预览,预览时可以进行缩放,并且可以删除选中状态的图片 .很不错的源码,大家有需要可以下载看看 . 微信 微信 微信 ...

  4. Django项目:CRM(客户关系管理系统)--61--51PerfectCRM实现CRM客户报名流程学生合同上传照片

    # sales_views.py # ————————47PerfectCRM实现CRM客户报名流程———————— from django.db import IntegrityError # 主动 ...

  5. Iphone H5上传照片被旋转

    最近做项目发现在Iphone下,我们上传图片都会被翻转,最后查阅资料发现,的确是IOS的问题 不说过程,直接解决方法 iOS下,html方式使用<input type="file&qu ...

  6. Android使用XUtils框架上传照片(一张或多张)和文本,server接收照片和文字(无乱码)

    Android上传图片,这里我使用了如今比較流行的XUtils框架.该框架能够实现文件上传.文件下载.图片缓存等等,有待研究. 以下是Android端上传的代码: xUtils.jar下载 Strin ...

  7. requests模拟上传照片

    博客园相册管理中有上传照片的功能 现在通过requests库模拟上传图片功能 先手动上传图片,用Fiddler转包,查看到上传图片接口请求格式, ------WebKitFormBoundarySKZ ...

  8. java jfinal + ajaxfileupload.js 上传

    功能上传 需求:同时上传多张图片 前端:jquery.ajaxfileupload.js 后端:jfinal upload.htm <html> <body> <div ...

  9. android之使用GridView+仿微信图片上传功能

    由于工作要求最近在使用GridView完成图片的批量上传功能,我的例子当中包含仿微信图片上传.拍照.本地选择.相片裁剪等功能,如果有需要的朋友可以看一下,希望我的实际经验能对您有所帮助. 直接上图,下 ...

随机推荐

  1. React.js学习小结

    最近一段时间都在学习React.js,感觉还不错,现在把自己的一些学习笔记记录一下,留着以后学习查看. 0.React全家桶(技术栈) 1.React主体 2.WebPack:grunt.gulp自动 ...

  2. 如何用 windows+github搭建一个优美的hexo博客

    1.Hexo简单介绍 Hexo 是一个快速.简洁且高效的博客框架.Hexo 使用 Markdown(或其他渲染引擎)解析文章,在几秒内,即可利用靓丽的主题生成静态网页. 风一般的速度Hexo基于Nod ...

  3. VMware 安装提示缺少MicrosoftRuntime DLL 问题解决办法

    VMware 安装提示缺少MicrosoftRuntime DLL 问题解决办法 刚刚安装VMware失败了试了好多办法,在这总结一下. 下面是程序的截图 这是报错信息 网上的解决方法: 当出现安装失 ...

  4. 测试css3的动画效果在display:none的时候不耗费性能

    也许你也有这个疑惑,动画一直在播放,那它不显示出来的时候也一直在播放的话,那是否一直占用资源呢? <!doctype html> <html> <head> < ...

  5. (转)防止ViewPager中的Fragment被销毁的方法

    在使用ViewPager与Fragment的时候,ViewPager会自动缓存1页内的数据,如下图: 当我们当前处在页面2的时候,页面1和页面3的View实际上已经创建好了,所以在我们拖动的时候是可以 ...

  6. MySQL几个join

    1.因为关系型数据库的基本原理,是基于“关系代数”.最重要的一类关系代数,就是2个集合之间的运算. 从集合运算的视角,去理解SQL中的几个常用join (1)inner join (2)left jo ...

  7. sqlserver row_number函数的用法

    ROW_NUMBER()函数将针对SELECT语句返回的每一行,从1开始编号,赋予其连续的编号 必须和over一起使用 select *,ROW_NUMBER() over(order by prod ...

  8. Selenium2学习(十八)-- js处理内嵌div滚动条

    前言 前面有篇专门用js解决了浏览器滚动条的问题,生活总是多姿多彩,有的滚动条就在页面上,这时候又得仰仗js大哥来解决啦. 一.内嵌滚动条 1.下面这张图就是内嵌div带有滚动条的样子,记住它的长相. ...

  9. 3元购买微信小程序解决方案一个月

    一.登录微信公众平台https://mp.weixin.qq.com/ 二.点击立即注册.注意:这里不要用微信公众号登录,小程序账号和微信公众号是不同的. 三.在注册页面点击小程序板块. 四.进入小程 ...

  10. CSS中的EM属性之弹性布局

    这篇教程将引导大家如何使用“em”来创建一个基本的弹性布局,从而学习其如何计算?又是如何使用“em”对层进行弹性扩展?又是如何扩展文本和图像等内容?下在我们就一起带着这些问题开始今天的“em”之行. ...