上传插件的使用

来源:互联网 发布:交易模拟软件 编辑:程序博客网 时间:2024/06/05 10:03
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<link href="<%=request.getContextPath()%>/pub/js/uploadify3.2/uploadify.css" type="text/css" rel="stylesheet" />
    
<script type="text/javascript" src="<%=request.getContextPath()%>/pub/js/uploadify3.2/jquery.uploadify.min.js" ></script>
 
<script type="text/javascript">
/**
 *
 * @param fileId  文件元素标识

 * @param resultId  上传文件结果保存元素id(<input type='hidden' id="uploadFiles" name="uploadFiles");
 *          
 * @param multi  是否支持多文件上传,true false或数字
  true:支持,最多10张; false:不支持,最多一张; 数值n:大于1,支持,最多n张; 小于1,不支持,最多一张
 *          
 * @param comFlag 是否生成缩略图
 *          
 * @param s_width 生成缩略图宽度
 *           
 * @param s_height 生成缩略图高度
 *                    
 * @param onComplete 上传完成后处理函数
 *
 * @param fileTypeExts 允许上传的文件类型
 */
 function uploadFile(fileId,resultId,multi,comFlag,s_width,s_height,onComplete,fileTypeExts){
        var _multi = false;
        var path = '${pageContext.request.contextPath}';
        var _uploadLimit = 10;
        if(multi+'' == 'true'){
          _multi = true;
          _uploadLimit = 10;
        }else if(!isNaN(multi) && Number(multi) > 1 ){
            _multi = true;
            _uploadLimit = Number(multi);
        }

        if (!onComplete){
            onComplete = defaultUploadOnComplete;
        }
        if(!fileTypeExts){
            fileTypeExts = '*.gif;*.jpg;*.jpeg;*.png;*.bmp;*.txt;*.xls;*.zip;*.swf';
        }
        $('#'+fileId).uploadify({
            swf: path+'/pub/js/uploadify3.2/uploadify.swf',//[必须设置]swf的路径
            cancelImage: path+'/pub/js/uploadify3.2/uploadify-cancel.png',//[必须设置]取消图片的路径
            uploader: '/servlet/upload.do',//[必须设置]上传文件触发的url
            formData: {'comFlag':comFlag,'smallWidth':s_width,'smallHeight':s_height},
            method: 'post',//和后台交互的方式:post/get
            auto:true,//文件选择完成后,是否自动上传
            langFile: path+'/pub/js/uploadify3.2/langFile.js',//语言包的路径,能设置所有的提示文字
            buttonText: $('#'+fileId).val() ? $('#'+fileId).val() : '请选择',//上传按钮的文字
            height: 15,//上传按钮的高和宽
            width: 60,
            multi: _multi,//是否能选择多个文件
            uploadLimit: _uploadLimit,//能同时上传的文件数目
            progressData : 'all', // 'percentage''speed''all'//队列中显示文件上传进度的方式:all-上传速度+百分比,percentage-百分比,speed-上传速度
            removeCompleted : true,//上传成功后的文件,是否在队列中自动删除
            removeTimeout: 1,
            fileTypeDesc: '文件',//允许上传的文件类型的描述,在弹出的文件选择框里会显示
            fileTypeExts: fileTypeExts,//允许上传的文件类型,限制弹出文件选择框里能选择的文件
            onUploadSuccess : function(file,data,response) {//上传完成时触发(每个文件触发一次)
            onComplete(data,resultId,multi);
            },
            onUploadError: function(file,errorCode,errorMsg,errorString) {//上传文件出错是触发(每个出错文件触发一次)
                alert( '上传文件发生错误: ' + errorString
                   + '\n错误代码: ' + errorCode
                   + ' ; 错误描述: ' + errorMsg
                   + '\nid: ' + file.id
                   + ' ; 索引: ' + file.index
                    + ' ; 文件名: ' + file.name
                   + ' ; 文件大小: ' + file.size
                    + ' ; 类型: ' + file.type
                    + ' ; 创建日期: ' + file.creationdate
                    + ' ; 修改日期: ' + file.modificationdate
                   + ' ; 文件状态: ' + file.filestatus);
        if(errorString){
          alert("上传文件终止,终止原因:"+errorString);
        }else{
          alert("上传文件意外终止。");
        }
              }
    });
}

  function defaultUploadOnComplete(data,objId,multi){
         try{
               data = eval('(' + data + ')');
  }catch(e){
    alert("上传图片返回异常:"+data);
    return;
  }

 if (data.error==1){
    alert(data.datas);
 }else if (data.error == 0 ){
   if (multi){
          var oldVal = $("#"+objId).val();
          if (oldVal != null && oldVal != "" ){
            oldVal = oldVal+";";
          }
       $("#"+objId).val(oldVal+data.datas);
   }else{
       $("#"+objId).val(data.datas);
   }
 }
}

</script>



$(document).ready(function(){
var userId = '${ADMIN_USER_INFO_NAME}';
if(userId == ''){
location.href='${pageContext.request.contextPath}/admin/index';
}
$("#saveBtn").click(doSave);
uploadFile("def_pic_btn", "def_pic",false, "yes", "200", "200",onComplete);
uploadFile("bro_pics_btn", "bro_pics",10, "yes", "200", "200");
});


0 0