MUI 單圖片壓縮上傳(拍照+系統相冊): 選擇當即上傳

1 html 部分javascript

<!DOCTYPE html>  
<html>  
    <head>  
        <meta charset="UTF-8">  
        <title></title>  
        <link href="../css/mui.min.css" rel="stylesheet" />  
        <script src="../js/mui.min.js"></script>  
        <script src="../js/jquery-1.9.min.js"></script>  
        <style>  
            .imageup{ position: absolute; margin:auto;left:0px;top:0;right:0;bottom:0; width:100px; height: 36px;}  
        </style>  
    </head>  
    <body>  
        <a href="javascript:;" class="imageup">上傳圖片</a>  
        <script>  
            
            function plusReady(){  
                // 彈出系統選擇按鈕框  
                mui("body").on("tap",".imageup",function(){  
                    page.imgUp();  
                })  
                  
            }  
            var page=null;  
            page={  
                imgUp:function(){  
                    var m=this;  
                    plus.nativeUI.actionSheet({cancel:"取消",buttons:[  
                        {title:"拍照"},  
                        {title:"從相冊中選擇"}  
                    ]}, function(e){//1 是拍照  2 從相冊中選擇  
                        switch(e.index){  
                            case 1:clickCamera();break;  
                            case 2:clickGallery();break;  
                        }  
                    });  
                }  
                //攝像頭  
            }  
              
              
     //發送照片  
    function clickGallery() {  
        plus.gallery.pick(function(path) {  
            plus.zip.compressImage({  
                src: path,  
                dst: "_doc/chat/gallery/" + path,  
                quality: 20,  
                overwrite: true  
            }, function(e) {  
                //服務端接口路徑
                var server = "http://www.test.cn/aaa.php";
                //打開等待旋轉提示按鈕
                var wt=plus.nativeUI.showWaiting();
                var task = plus.uploader.createUpload(server, {  
                    method: "post"  
                    }, function(t, status) {  
                        if(status == 200) { 
                            alert("上傳成功:"+t.responseText);
                                wt.close(); //關閉等待提示按鈕
                        }else{
                            alert("上傳失敗:"+status);
                            wt.close();//關閉等待提示按鈕
                        }
                    });  
                task.addFile(e.target, {}); 
                //添加其餘參數
                task.addData("name","test");
                task.start();  
            }, function(err) {  
                console.error("壓縮失敗:" + err.message);  
            });  
  
        }, function(err) {});  
    };  
      
      
    // 拍照  
    function clickCamera() {  
        var cmr = plus.camera.getCamera();  
        var res = cmr.supportedImageResolutions[0];  
        var fmt = cmr.supportedImageFormats[0];  
        cmr.captureImage(function(path) {  
            
            plus.io.resolveLocalFileSystemURL(path, function(entry) {  
                var localUrl = entry.toLocalURL();  
                plus.zip.compressImage({  
                    src: localUrl,  
                    dst: "_doc/chat/camera/" + localUrl,  
                    quality: 20,  
                    overwrite: true  
                }, function(e) {  
                    //服務端接口路徑
                    var server = "http://www.test.cn/aaa.php";
                    //打開等待旋轉提示按鈕
                    var wt=plus.nativeUI.showWaiting();
                    var task = plus.uploader.createUpload(server, {  
                        method: "post"  
                    }, function(t, status) {  
                        if(status == 200) { 
                            alert("上傳成功:"+t.responseText);
                                wt.close(); //關閉等待提示按鈕
                        }else{
                            alert("上傳失敗:"+status);
                            wt.close();//關閉等待提示按鈕
                        }
                    });  
                    task.addFile(e.target, {});  
                    //添加其餘參數
                    task.addData("name","test");
                    task.start();  
                }, function(err) {  
                    console.log("壓縮失敗:  " + err.message);  
                });  
            });  
        }, function(err) {  
            console.error("拍照失敗:" + err.message);  
        }, {  
            index: 1  
        });  
    };  
  
      
     if(window.plus){  
        plusReady();  
    }else{  
        document.addEventListener("plusready",plusReady,false);  
    }       
      
     
        </script>  
    </body>   
</html>  

 

 2 服務端文件(PHP)php

<?php 

echo "<pre>";
print_r($_POST);

print_r($_FILES);


 ?>

 

 測試結果如圖:css

相關文章
相關標籤/搜索