微信上傳圖片


低版本的安卓上傳圖片是個問題,能出現選擇圖片,但點擊圖片后沒有反應,轉成base64也無解。於是改為用微信的接口上傳。和之前的微信分享功能都是基於微信的jssdk。

步驟比我們平時上傳到服務器多一步,他是先調用chooseeImage方法獲得用戶要上傳的圖片id。然后上傳到微信的服務器,微信的服務器默認只保存三天,所以還要讓后台下載到自己的服務器上,然后返回地址,前端顯示,這樣才算完成。

     var time = '@ViewBag.Share.timestamp';
      wx.config({
        debug: false, // 開啟調試模式,調用的所有api的返回值會在客戶端alert出來,若要查看傳入的參數,可以在pc端打開,參數信息會通過log打出,僅在pc端時才會打印。
        appId: '@ViewBag.Share.appId', // 必填,公眾號的唯一標識
        timestamp: parseInt(time), // 必填,生成簽名的時間戳
        nonceStr: '@ViewBag.Share.nonceStr', // 必填,生成簽名的隨機串
        signature: '@ViewBag.Share.signature',// 必填,簽名,見附錄1
          jsApiList: ["chooseImage", "previewImage", "uploadImage", "downloadImage"] // 必填,需要使用的JS接口列表,所有JS接口列表見附錄2
      });
        wx.ready(function() {
            $(document).on("click", ".ctcon", function() {
                wx.chooseImage({
                    count: 1, // 默認9
                    sizeType: ['original', 'compressed'], // 可以指定是原圖還是壓縮圖,默認二者都有
                    sourceType: ['album', 'camera'], // 可以指定來源是相冊還是相機,默認二者都有
                    success: function (res) {
                        var localIds = res.localIds; // 返回選定照片的本地ID列表,localId可以作為img標簽的src屬性顯示圖片
                        uploadimg(localIds[0].toString());

                    }
                });
            });

                
function uploadimg(lid) { wx.uploadImage({ localId: lid, // 需要上傳的圖片的本地ID,由chooseImage接口獲得 isShowProgressTips: 1, // 默認為1,顯示進度提示 success: function (res) { var serverId = res.serverId; // 返回圖片的服務器端ID //alert(serverId); $.post("/Question/DownWxImage", { serverId: serverId }, function(res) { //alert(res.SaveName); if (res.Success === true) { // 顯示圖片 } }); }); } });

count表示讓用戶選擇圖片的張數,然后這里的localIds要tostring。不然上傳不了。uploadImage執行完了就可以通知讓后台上傳:

  public ActionResult DownWxImage(string serverId)
        {
            var token = getToken();
             var url = string.Format("http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={0}&media_id={1}",
                 token, serverId);//圖片下載地址

             HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);

             req.Method = "GET";
             using (WebResponse wr = req.GetResponse())
             {
                 HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();

                var strpath = myResponse.ResponseUri.ToString();
                 WebClient mywebclient = new WebClient();

                 var path = "/Content/UploadFiles/mobile/";
                 var uploadpath = Server.MapPath(path);
                 if (!Directory.Exists(uploadpath))
                 {
                     Directory.CreateDirectory(uploadpath);
                 }
                 string saveName = Encrypt.GenerateOrderNumber() + ".jpg";
                 var savePath = uploadpath + saveName;

                 try
                 {
                     mywebclient.DownloadFile(strpath, savePath);
                     return Json(new { Success = true, SaveName = path + saveName });
                 }
                 catch (Exception ex)
                 {
                     savePath = ex.ToString();
                 }

             }
             return Json(new {Success = false, Message = "上傳失敗!"});
        }

 這樣安卓是能上傳了,但是也沒有了進度條。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM