. Net mvc uses Senparc. Weixin to upload images,

Source: Internet
Author: User

. Net mvc uses Senparc. Weixin to upload images,

First, obtain the signature. Controller code:

Public ActionResult MutualHelpAdd () {var jssdkUiPackage = JSSDKHelper. getJsSdkUiPackage (WeChatParameter. _ appID, WeChatParameter. _ appSecret, Request. url. absoluteUri); ViewBag. appId = WeChatParameter. _ appID; ViewBag. timestamp = jssdkUiPackage. timestamp; ViewBag. nonceStr = jssdkUiPackage. nonceStr; ViewBag. signature = jssdkUiPackage. signature; return View ();}View Code

View code:

Wx. config ({// debug: true, appId: '@ ViewBag. appId ', timestamp:' @ ViewBag. timestamp ', nonceStr:' @ ViewBag. nonceStr ', signature:' @ ViewBag. signature ', jsApiList: ['chooseimag', 'previewimag', 'uploadimag', 'downloadimage']}); wx. ready (function () {// 5 image interface // 5.1 Image taking, Local Image Selection var images = {localId: [], serverId: []}; document. querySelector ('# btnaddimage '). onclick = function () {wx. chooseImage ({ Count: 3, // The default value is 9. Set the number of images that can be uploaded simultaneously. sizeType: ['original', 'computed']. // you can specify the source image or compressed image, both have sourceType by default: ['alipay', 'camera '], // you can specify whether the source is an album or a camera. By default, both have success: function (res) {images. localId = res. localIds; // alert ('selected' + res. localIds. length + 'image'); var I = 0, length = images. localId. length; images. serverId = []; function upload () {wx. uploadImage ({localId: images. localId [I], success: functi On (res) {I ++; // alert ('uploaded: '+ I +'/'+ length); images. serverId. push (res. serverId); // TODO: res. serverId is media_id, which reads image data from the server: Custom upload to your server var imageJson = $ ("# hdImageJson "). val (); $. ajax ({type: 'post', url: '/Common/AddImage', async: false, data: {media_id: res. serverId, imageJson: imageJson, folderName: "MutualHelp"}, success: function (data) {if (data. imageUrl! = '') {// Return the image in the url var img =" How to save images to a local server:

/// <Summary> /// Public Image Upload method /// </summary> /// <param name = "media_id"> </param> /// <param name = "imageJson"> </param> // <returns> </returns> public JsonResult AddImage (string media_id, string imageJson, string folderName) {var imgUrl = ""; try {List <ImageList> curImageResult; if (! String. isNullOrEmpty (imageJson) {curImageResult = JsonConvert. deserializeObject <List <ImageList> (imageJson);} else {curImageResult = new List <ImageList> ();} var accessToken = AccessTokenContainer. tryGetAccessToken (WeChatParameter. _ appID, WeChatParameter. _ appSecret); System. IO. memoryStream stream = new System. IO. memoryStream (); MediaApi. get (accessToken, media_id, stream); Image img = Image. fromStream (stream); var imgName = DateTime. now. toString ("yyyyMMddhhss"); var fileSaveUrl = string. format (FilePath + "/{0}/" + imgName + ". jpg ", folderName); imgUrl = string. format ("/{0}/" + imgName + ". jpg ", folderName); img. save (fileSaveUrl); var imageEntity = new ImageList {ImageKey = imgName, ImageUrl = imgUrl, UploadTime = DateTime. now}; curImageResult. add (imageEntity); imageJson = JsonConvert. serializeObject (curImageResult);} catch (Exception ex) {imgUrl = "";} var result = new {ImageJson = imageJson, ImageUrl = imgUrl}; return Json (result );}View Code

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.