使用微信JS-SDK圖像接口上傳圖片簡單實例-創(chuàng)新互聯(lián)

1.完整實現(xiàn)ios和安卓微信環(huán)境選擇微信相冊圖片并上傳保存在本地
2.注意js版本兼容,使用該版本

七臺河ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18982081108(備注:SSL證書合作)期待與您的合作!
<script src="http://res.wx.qq.com/open/js/jweixin-1.2.0.js" type="text/javascript"></script>

官方解釋
使用微信JS-SDK圖像接口上傳圖片簡單實例
3.本實例流程將先調(diào)用wx.chooseImage接口獲取選擇的圖片localId,返回localId在安卓微信可以作為img標簽的src屬性顯示圖片,但在ios下需要再調(diào)用wx.getLocalImgData接口來顯示圖片,然后要保存圖片需調(diào)用wx.uploadImage接口將圖片上傳至微信服務器返回serverId,將serverId放至隱藏域提交后端,由后端調(diào)用微信多媒體接口下載保存到自己服務器,多媒體獲取接口為:

http://file.api.weixin.qq.com/cgi-bin/media/get?access_token="token()"&media_id="serverId"

4.前端核心代碼如下:

.....
.....
        <div class="upload-box">
            <div  class="z_file">
                <div id="uploaderBox"></div>
            </div>
            {volist name='question.reply_img' id='item'}
                {if condition="$item"}
                  <div class="z_addImg">
                    <a class="shc-btn shc-btn-id" href="javascript:;"></a>
                    <img src="{$item|default=""}">
                    <input name="edit_img_key[]" value="{$item}"></input>
                  </div>
                {/if}
            {/volist}
        </div>
.....
.....
wx.config({
            // 配置信息, 即使不正確也能使用 wx.ready
            debug: false,
            appId:"{$signPackage.appId}",
            timestamp:"{$signPackage.timestamp}",
            nonceStr:"{$signPackage.nonceStr}",
            signature:"{$signPackage.signature}",
            jsApiList: [
                // 所有要調(diào)用的 API 都要加到這個列表中
                'uploadImage',
                'chooseImage',
                'getLocalImgData',
            ]
        });
        var imgContainer = document.getElementsByClassName("upload-box")[0];
        $("#uploaderBox").on("click", function(e) {
           wx.chooseImage({
                count: 9, // 默認9
                sizeType: ['compressed'], // 可以指定是原圖還是壓縮圖,默認二者都有
                sourceType: ['album', 'camera'], // 可以指定來源是相冊還是相機,默認二者都有
                success: function (res) {
                    var localIds = res.localIds; // 返回選定照片的本地ID列表,localId可以作為img標簽的src屬性顯示圖片
                    var i = 0, length = localIds.length;//循環(huán)操作9張圖片
                    function upload() {
                        var serverId = '';
                       wx.uploadImage({
                            localId: localIds[i], // 需要上傳的圖片的本地ID,由chooseImage接口獲得
                            isShowProgressTips: 1, // 默認為1,顯示進度提示
                            success: function (res) {
                                serverId = res.serverId; // 返回圖片的服務器端ID
                                if(agent_type == 'android')
                                {
                                    var localData = localIds[i]; // localData是圖片的base64數(shù)據(jù),可以用img標簽顯示
                                    //追加html
                                    var img = document.createElement("img");
                                    var input = document.createElement("input");
                                    var ass = document.createElement("a");
                                    img.setAttribute("src", localData);
                                    input.setAttribute("name", "serverId[]");
                                    input.setAttribute("value", serverId);
                                    var imgAdd = document.createElement("div");
                                    imgAdd.setAttribute("class", "z_addImg");
                                    ass.setAttribute("class", "shc-btn");
                                    ass.setAttribute("id", "shc-btn"+id);
                                    ass.setAttribute("href", "javascript:;");
                                    imgAdd.appendChild(ass);
                                    imgAdd.appendChild(img);
                                    imgAdd.appendChild(input);
                                    imgContainer.appendChild(imgAdd);
                                    imgRemove(id);//刪除按鈕
                                    id++;
                                }
                                else
                                {
                                   wx.getLocalImgData({
                                        localId: localIds[i], // 圖片的localID
                                        success: function (res) {
                                            var localData = res.localData; // localData是圖片的base64數(shù)據(jù),可以用img標簽顯示
                                            var img = document.createElement("img");
                                            var input = document.createElement("input");
                                            var ass = document.createElement("a");
                                            img.setAttribute("src", localData);
                                            input.setAttribute("name", "serverId[]");
                                            input.setAttribute("value", serverId);
                                            var imgAdd = document.createElement("div");
                                            imgAdd.setAttribute("class", "z_addImg");
                                            ass.setAttribute("class", "shc-btn");
                                            ass.setAttribute("id", "shc-btn"+id);
                                            ass.setAttribute("href", "javascript:;");
                                            imgAdd.appendChild(ass);
                                            imgAdd.appendChild(img);
                                            imgAdd.appendChild(input);
                                            imgContainer.appendChild(imgAdd);
                                            imgRemove(id);
                                            id++;
                                        }
                                    });
                                }
                                i++;
                                if (i < length) {
                                    upload();
                                }
                            }
                        });
                    }
                    upload();
                }
            });
        });

5.后端核心代碼如下:

foreach ($data['serverId'] as $key => $value)
            {
                $str = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=".獲取token."&media_id=".$value;
                $a = file_get_contents($str);
                if($a)
                {
                    $resource = fopen(ROOT_PATH."/uploads/".$value.".jpg" , 'w+');
                    fwrite($resource, $a);
                    fclose($resource);
                    $imgs[] = "/uploads/".$value.".jpg";
                }
            }

效果截圖
使用微信JS-SDK圖像接口上傳圖片簡單實例

另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。

新聞名稱:使用微信JS-SDK圖像接口上傳圖片簡單實例-創(chuàng)新互聯(lián)
文章分享:http://muchs.cn/article42/ddoshc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、定制開發(fā)、標簽優(yōu)化網(wǎng)站改版、域名注冊響應式網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

網(wǎng)站優(yōu)化排名