演示效果參考:http://www.erdangjiade.com/js/880.html
上傳頭像區域
上傳頭像區域
<div class="avatar_area"> <a href="javascript:void(0);"class="avatar_uplpad_btn" id="avatar_uplpad_btn"></a> <div id="avatar_pic"> <img alt="充值" src="http://www.erdangjiade.com/Public/images/other/banner.jpg"/> </div> </div>引入jQuery庫和plupload上傳元件
<script type="text/javascript" src="jquery.js"></script> <script type="text/javascript" src="plupload/plupload.full.min.js"></script>plupload單圖片上傳配置
var uploader_avatar = new plupload.Uploader({//建立例項的構造方法 runtimes: 'gears,html5,html4,silverlight,flash', //上傳外掛初始化選用那種方式的優先順序順序 browse_button: ['avatar_uplpad_btn'], // 上傳按鈕 url: "ajax.php", //遠端上傳地址 flash_swf_url: 'js/plugins/plupload/Moxie.swf', //flash檔案地址 silverlight_xap_url: 'js/plugins/plupload/Moxie.xap', //silverlight檔案地址 filters: { max_file_size: '10mb', //最大上傳檔案大小(格式100b, 10kb, 10mb, 1gb) mime_types: [//允許檔案上傳型別 {title: "files", extensions: "jpg,png,gif,jpeg"} ] }, multi_selection: false, //true:ctrl多檔案上傳, false 單檔案上傳 init: { FilesAdded: function(up, files) { //檔案上傳前 uploader_avatar.start(); }, UploadProgress: function(up, file) { //上傳中,顯示進度條 var percent = file.percent; $("#" + file.id).find('.bar').css({"width": percent + "%"}); $("#" + file.id).find(".percent").text(percent + "%"); }, FileUploaded: function(up, file, info) { //檔案上傳成功的時候觸發 var data = eval("(" + info.response + ")");//解析返回的json資料 $("#avatar_pic").html("<img src='" + data.pic + "'/>") }, Error: function(up, err) { //上傳出錯的時候觸發 alert(err.message); } } }); uploader_avatar.init();