調(diào)用示例

2018-02-08 10:17 更新
  • 后端

/* 上傳后端代碼示例 */

...

include_once libfile('class/uploadhandler'); //加載類(lèi)(詳細(xì)的實(shí)現(xiàn)代碼和參數(shù)請(qǐng)參看:./core/class/class_uploadhandler.php)

$space=dzzgetspace($_G['uid']); //獲取當(dāng)前用戶(hù)的設(shè)置信息

$allowedExtensions = $space['attachextensions']?explode(',',$space['attachextensions']):array(); //允許附件類(lèi)型

// max file size in bytes

$sizeLimit =intval($space['maxattachsize']);  //最大上傳單個(gè)文件大小

$options=array(

'param_name'=>'upfile',

               'accept_file_types'=>$allowedExtensions?("/(\.|\/)(".implode('|',$allowedExtensions).")$/i"):"/.+$/i",

'max_file_size'=>$sizeLimit?$sizeLimit:null,

'upload_dir' =>$_G['setting']['attachdir'].'cache/',

'upload_url' => $_G['setting']['attachurl'].'cache/',

                'thumbnail'  =>array('max-width'=>130,'max-height'=>160)   //生成縮略圖的寬度和高度

     );

$upload_handler = new uploadhandler($options);

...


  • 前端示例(此示例為評(píng)論上傳示例代碼,詳細(xì)請(qǐng)查看./dzz/comment/template/publish_form.htm)

/* 上傳前端代碼示例 */

...

<script type="text/javascript" src="dzz/scripts/jquery-1.10.2.min.js"></script>

<!-- The jQuery UI widget factory, can be omitted if jQuery UI is already included -->

<script type="text/javascript" src="dzz/scripts/jquery_file_upload/jquery.ui.widget.js?{VERHASH}"></script>

<script type="text/javascript" src="dzz/scripts/jquery_file_upload/jquery.iframe-transport.js?{VERHASH}"></script>

<!-- The basic File Upload plugin -->

<script type="text/javascript" src="dzz/scripts/jquery_file_upload/jquery.fileupload.js?{VERHASH}"></script>

<script type="text/javascript" src="dzz/scripts/jquery_file_upload/jquery.fileupload-process.js?{VERHASH}"></script>

<script type="text/javascript" src="dzz/scripts/jquery_file_upload/jquery.fileupload-validate.js?{VERHASH}"></script>

...

var attachextensions='{eval echo implode('|',$space[attachextensions]);}';

   if(attachextensions=='') attachextensions="\.*$";

else attachextensions="(\.|\/)("+(attachextensions)+")$";

    jQuery('#fileupload_0').fileupload({

        url: DZZSCRIPT+'?mod=comment&op=ajax&do=upload',

        dataType: 'json',

autoUpload: true,

maxChunkSize:(parseInt('{$_G[setting][maxChunkSize]}') || 2000000), //分塊大小

dropZone:jQuery('#publish_0'),   //拖放上傳的區(qū)域

pasteZone:jQuery('#publish_0'),  //粘貼上傳區(qū)域

maxFileSize: parseInt('{$space[maxattachsize]}')>0?parseInt('{$space[maxattachsize]}'):null, // 最大單個(gè)文件大小

acceptFileTypes:new RegExp(attachextensions,'i'),   //允許的文件類(lèi)型

add:function(e,data){

data.context = jQuery('<div/>').appendTo('#attachmentViewBox_0');

jQuery.each(data.files, function (index, file) {

if(!file.name) file.name='clipboardData.png';

var html='';

html+=' <div  class="attachment_previewer">';

html+='     <div class="attachmentviewbox">';

html+='         <div class="view_attvb clearfix">';

html+='           <div class="ico_vattvb "><img alt="'+file.name+'" src="dzz/images/default/upload_failure.png" style="height:50px"></div>';

html+='    <div class="ico_vattvb_right">';

html+='             <div class="ico_name">'+file.name+'</div>';

html+='             <div class="progress  active" style="margin:0;" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="progress-bar progress-bar-success" style="width:0%;"></div></div>';

html+='          </div>';

html+='       </div>';

html+='    </div>';

html+=' </div>';

jQuery(html).appendTo(data.context);

});

data.process().done(function () {

data.submit();

});

},

progress: function (e,data){

var index = 0;//data.index,

                         // file = data.files[index],

                           var  node = jQuery(data.context.children()[index]);

 var progress = parseInt(data.loaded / data.total * 100, 10);

node.find('.progress-bar').css(

'width',

progress + '%'

);

},

        done: function (e, data) {

            jQuery.each(data.result.files, function (index, file) {

if(file.error){

jQuery(data.context.children()[index]).find('.progress').replaceWith('<span class="text-danger">'+file.error+'</span>');

}else{

feed_addAttach(file.data,jQuery(data.context.children()[index]),'0');

}

            });

        }

    });

...

以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)