调用示例
后端
/* 上传后端代码示例 */
...
include_once libfile('class/uploadhandler'); //加载类(详细的实现代码和参数请参看:./core/class/class_uploadhandler.php)
$space=dzzgetspace($_G['uid']); //获取当前用户的设置信息
$allowedExtensions = $space['attachextensions']?explode(',',$space['attachextensions']):array(); //允许附件类型
// max file size in bytes
$sizeLimit =intval($space['maxattachsize']); //最大上传单个文件大小
$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);
...
前端示例(此示例为评论上传示例代码,详细请查看./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'), //拖放上传的区域
pasteZone:jQuery('#publish_0'), //粘贴上传区域
maxFileSize: parseInt('{$space[maxattachsize]}')>0?parseInt('{$space[maxattachsize]}'):null, // 最大单个文件大小
acceptFileTypes:new RegExp(attachextensions,'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');
}
});
}
});
...