上傳圖片外掛程式web upload

來源:互聯網
上載者:User

標籤:error   make   上傳檔案   javascrip   start   addclass   出錯   position   end   

 

官網地址:http://fex.baidu.com/webuploader/getting-started.html

 

html:

<!--dom結構部分--><div id="uploader-demo">    <!--用來存放item-->    <div id="fileList" class="uploader-list"></div>    <div id="filePicker">選擇圖片</div></div>

 

css3

.webuploader-container {    position: relative;}.webuploader-element-invisible {    position: absolute !important;    clip: rect(1px 1px 1px 1px); /* IE6, IE7 */    clip: rect(1px,1px,1px,1px);}.webuploader-pick {    position: relative;    display: inline-block;    cursor: pointer;    background: #00b7ee;    padding: 10px 15px;    color: #fff;    text-align: center;    border-radius: 3px;    overflow: hidden;}.webuploader-pick-hover {    background: #00a2d4;}.webuploader-pick-disable {    opacity: 0.6;    pointer-events:none;}

 

JavaScript

//id -> 存放圖片  width,height  存放圖片尺寸寬高  id2 按鈕  allMaxSize圖片大小function picUpload(id,width,height,id2,allMaxSize){    var $ = jQuery,        $list = $(id),    // 最佳化retina, 在retina下這個值是2        ratio = window.devicePixelRatio || 1,    // 縮圖大小        thumbnailWidth = width * ratio,        thumbnailHeight = height * ratio,    //allMaxSize = 1,//限制圖片大小    // Web Uploader執行個體        uploader;    // 初始化Web Uploader    uploader = WebUploader.create({        // 自動上傳。        auto: true,        // swf檔案路徑        swf: ‘E:/work/seller/public/img/common‘,        // 檔案接收服務端。        server: ‘http://webuploader.duapp.com/server/fileupload.php‘,        // 選擇檔案的按鈕。可選。        // 內部根據當前運行是建立,可能是input元素,也可能是flash.        pick: id2,        duplicate :true,//是否可以重複上傳        fileSizeLimit: allMaxSize*1024*1024, //限制圖片大小,不可以超過        compress: null,        // 只允許選擇檔案,可選。        accept: {            title: ‘Images‘,            extensions: ‘gif,jpg,jpeg,bmp,png‘,            mimeTypes: ‘image/jpg,image/jpeg,image/png‘   //修改這行,加快上傳檔案框的開啟速度        }    });    // 當有檔案添加進來的時候    uploader.on( ‘fileQueued‘, function( file ) {        $list.empty(); //清空之前上傳的檔案        var $li = $(                ‘<div id="‘ + file.id + ‘" class="file-item thumbnail">‘ +                ‘<img>‘ +                ‘<div class="info">‘ + file.name + ‘</div>‘ +                ‘</div>‘            ),            $img = $li.find(‘img‘);        $list.append( $li );        // 建立縮圖        uploader.makeThumb( file, function( error, src ) {            if ( error ) {                $img.replaceWith(‘<span>不能預覽</span>‘);                return;            }            //去掉預設的頭像,清空圖片名字的資訊            $(‘.webuploader-pick img‘).attr(‘src‘,‘‘)            $(‘.file-item .info‘).empty();            $img.attr( ‘src‘, src );        }, thumbnailWidth, thumbnailHeight );    });    // 檔案上傳過程中建立進度條即時顯示。    uploader.on( ‘uploadProgress‘, function( file, percentage ) {        var $li = $( ‘#‘+file.id ),            $percent = $li.find(‘.progress span‘);        // 避免重複建立        if ( !$percent.length ) {            $percent = $(‘<p class="progress"><span></span></p>‘)                .appendTo( $li )                .find(‘span‘);        }        $percent.css( ‘width‘, percentage * 100 + ‘%‘ );    });    // 檔案上傳成功,給item添加成功class, 用樣式標記上傳成功。    uploader.on( ‘uploadSuccess‘, function( file ) {        $( ‘#‘+file.id ).addClass(‘upload-state-done‘);    });    // 檔案上傳失敗,現實上傳出錯。    uploader.on( ‘uploadError‘, function( file ) {        var $li = $( ‘#‘+file.id ),            $error = $li.find(‘div.error‘);        // 避免重複建立        if ( !$error.length ) {            $error = $(‘<div class="error"></div>‘).appendTo( $li );        }        //$error.text(‘上傳失敗‘);    });    // 完成上傳完了,成功或者失敗,先刪除進度條。    uploader.on( ‘uploadComplete‘, function( file ) {        $( ‘#‘+file.id ).find(‘.progress‘).remove();    });    //  驗證大小    uploader.on("error",function (type){         if(type == "Q_EXCEED_SIZE_LIMIT"){            window.alert("系統提示:所選圖片總大小不可超過" + allMaxSize + "M哦!換個小點的檔案吧!");        }        // 判斷傳入的是不是函數        /*  if( jQuery.isFunction(sideFun) ){         sideFun(type,allMaxSize);         }else {         //不是函數         console.log(2)         return false;         }*/        /*  if(sideFun === ‘undefined‘){         return true;         }else{         sideFun(type,allMaxSize);         }         */    });}//調用picUpload(‘#fileList‘,100,100,‘#filePicker‘,10000);

 

上傳圖片外掛程式web upload

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.