var newle = 0;//最新文件个数 var isff = 0;//非法文件数 $.fn.bindUpload = function () { var myData = { url: arguments[0].url || "", type: arguments[0].type || "", num: arguments[0].num || "", callbackPath: arguments[0].callbackPath || "#callbackPath", size: arguments[0].size || 5 } var _this = this; var input = this.find('input.add'); var sike = 0; var itemList = null; var status = []; var reg = /[\u4e00-\u9fa5]/g; if (myData.num == 1) { input.removeAttr("multiple"); } else { input.attr("multiple"); } //打开上传进度窗口 this.openWindow = function (files) { if (files.length > myData.num) { parent.ShowMsg("单次导入最多不超过" + myData.num + "张,本次预加载" + myData.num + "张!"); } var allSize = 0; for (var x = 0; x < files.length; x++) { if (myData.num && x < myData.num || !myData.num) { allSize += files[x].size; newle++; status.push({ name: files[x].name, state: 0 }); } else { status.push({ name: files[x].name, state: 1 }); } } var box = '
' + '
' + '
' + // ' 共' + files.length + '个文件 ' + ((allSize / 1048576).toFixed(3) == 0 ? allSize / 1048576 : (allSize / 1048576).toFixed(3)) + 'mb' + '图 片 预 览' + '
' + '
' + '
    ' + '
    ' + '
    ' + ' 全部上传' + ' 关闭预览' + '
    ' + '
    ' + '
    '; _this.append(box); //关闭进度窗口函数 _this.find(".off").bind("click", function () { newle = 0;//最新文件个数 isff = 0;//非法文件数 input.val(""); _this.find("#zwb_upload_status").fadeOut(100, function () { $(this).remove(); }); status = []; files = null; }); _this.getImgSrc(files); } var aa = 0; //显示缩略图函数 this.getImgSrc = function (files) { aa = 0; var reader = new FileReader(); var size = files[sike].size; var name = files[sike].name; var index1 = name.lastIndexOf("."); var type = name.substring(index1 + 1, name.length);//后缀名 var info = ""; if (myData.type != "" && myData.type.indexOf(type) == -1) { info = "文件格式非法"; status[sike].state = 1;//禁止上传 isff++; } //if (reg.test(name)) { // info = "文件名不能含有中文"; // status[sike].state = 1;//禁止上传 //} var filename = name.substring(0, name.lastIndexOf("."));//不带后缀的文件名 var ajaxUrl = "/PictureMgr/Picture/ExistPicName"; var ajaxData = { picName: filename, imgGuid: "" }; var msg = AjaxManagers(JSON.stringify(ajaxData), ajaxUrl); var resultstr = eval("(" + msg + ")"); if (resultstr.Success == "true" || resultstr.Success == true) { info = "图片名与后台重复"; status[sike].state = 1;//禁止上传 isff++; newle--; } if (myData.size != "" && (size / 1048576) > myData.size) { info = "文件过大"; isff++; status[sike].state = 1;//禁止上传 newle--; } if (type == "png" || type == "jpg" || type == "bmp" || type == "jpeg") { reader.readAsDataURL(files[sike]); reader.onload = function () { var imgPath = reader.result; if (sike < myData.num || myData.num == "") { _this.find("#zwb_upload_status ul.listBox").append('
  • ' + '' + '' + '

    ' + name + '

    ' + '

    大小: ' + ((size / 1048576).toFixed(3) == 0 ? size / 1048576 : (size / 1048576).toFixed(3)) + 'MB

    ' + '

    状态: 未上传

    ' + '
    ' + '' + '' + '
    ' + info + '
    ' + '
    ' + '
    ' + '
    ' + '
  • '); } sike++; if (sike == files.length) { sike = 0; _this.find(_this.find('#zwb_upload_status').fadeIn(100)); itemList = _this.find("#zwb_upload_status ul.listBox li"); // newle = itemList.length;//设置文件原始数量 _this.find(".ok").bind("click", function () { //if (isff > 0) { // parent.ShowMsg("请删除不合格文件后继续!"); // return false; //} if (newle <= 0) { parent.ShowMsg("无可上传图片,请关闭后重新选择图片上传!"); return false; } var lengthI = files.length; _this.find(".ok")[0].innerHTML = "正在上传,请勿关闭…"; _this.find(".ok").off("click"); _this.find(".ok").css("color", "red"); for (var i = 0; i < files.length; i++) { if (status[i].state == 0) { var imgHeight = _this.find("#zwb_upload_status ul.listBox").eq(0).find("img")[i].naturalHeight; var imgWidth = _this.find("#zwb_upload_status ul.listBox").eq(0).find("img")[i].naturalWidth; _this.uploadimg(lengthI, i, files[i], imgHeight, imgWidth); } } }); _this.find(".delete").bind("click", function () { var index = $(this).parent().index(); if (status[index].state == 1) { isff--; } if (status[index].state == 0) { newle--;//点击删除时文件数量递减 } status[index].state = 1; $(this).parent().hide(100); }); } else { return _this.getImgSrc(files); } }; } else { if (type == "zip" || type == "rar" || type == "jar" || type == "7z" || type == "cab" || type == "iso") { imgPath = 'img/zip.png'; } else if (type == "text") { imgPath = 'img/text.png'; } else if (type == "doc") { imgPath = 'img/word.png'; } else if (type == "html" || type == "htm" || type == "url") { imgPath = 'img/html.png'; } else if (type == "xls") { imgPath = 'img/excal.png'; } else if (type == "exe") { imgPath = 'img/exe.png'; } else if (type == "css" || type == "less" || type == "sass") { imgPath = 'img/css.jpg'; } else if (type == "js") { imgPath = 'img/js.jpg'; } else if (type == "php") { imgPath = 'img/php.png'; } else if (type == "sql") { imgPath = 'img/sql.png'; } else if (type == "ttf" || type == "otf" || type == "ttc" || type == "woff" || type == "woff2") { imgPath = 'img/font.png'; } else { imgPath = 'img/unknown.png'; } if (sike < myData.num || myData.num == "") { _this.find("#zwb_upload_status ul.listBox").append('
  • ' + '' + '' + '

    ' + name + '

    ' + '

    大小: ' + ((size / 1048576).toFixed(3) == 0 ? size / 1048576 : (size / 1048576).toFixed(3)) + 'b

    ' + '

    状态: 未上传

    ' + '
    ' + '' + '' + '
    ' + info + '
    ' + '
    ' + '
    ' + '
    ' + '
  • '); } sike++; if (sike == files.length) { sike = 0; _this.find(_this.find('#zwb_upload_status').fadeIn(100)); itemList = _this.find("#zwb_upload_status ul.listBox li"); newle = itemList.length;//设置文件原始数量 //_this.find(".ok").bind("click", function () { // var lengthI = files.length; // for (var i = 0; i < files.length; i++) { // if (status[i].state == 0) { // _this.uploadimg(lengthI, i, files[i]); // } // } //}); //_this.find(".delete").bind("click", function () { // var index = $(this).parent().index(); // status[index].state = 1; // $(this).parent().hide(100); //}); _this.find(".ok").bind("click", function () { var lengthI = files.length; //if (isff > 0) { // parent.ShowMsg("请删除不合格文件后继续!"); // return false; //} _this.find(".ok")[0].innerHTML = "正在上传,请勿关闭…"; _this.find(".ok").off("click"); _this.find(".ok").css("color", "red"); for (var i = 0; i < files.length; i++) { if (status[i].state == 0) { var imgHeight = _this.find("#zwb_upload_status ul.listBox").eq(0).find("img")[i].naturalHeight; var imgWidth = _this.find("#zwb_upload_status ul.listBox").eq(0).find("img")[i].naturalWidth; _this.uploadimg(lengthI, i, files[i], imgHeight, imgWidth); } } }); _this.find(".delete").bind("click", function () { var index = $(this).parent().index(); if (status[index].state == 1) { isff--; } status[index].state = 1; newle--;//点击删除时文件数量递减 $(this).parent().hide(100); }); } else { return _this.getImgSrc(files); } } } this.uploadimg = function (max, i, file, imgHeight, imgWidth) { //sizelj = sizelj + file.size; //console.log(aa); //console.log("****"); //console.log(newle); //console.log("sizelj:" + sizelj); //console.log("allSize:" + allSize); var reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function (e) { var ext = file.name.substring(file.name.lastIndexOf("."), file.name.length); $.ajax({ type: "POST", url: FileServerData.WebServiceURL + "/api/File/POSTUploadImg", //FileServerData.WebServiceURL获取文件服务器地址 在webconfig中有设置 data: { "base64": this.result, "type": ext, "watermark": watermarkguid, //获取水印图guid "watermarkShow": disType, //获取显示方式 "isMaxWatermark": bigNot, //获取大图是否需要水印 "isMinWatermark": smallNot, //获取小图是否需要水印 "maxWidth": bigSize, //获取大图尺寸 "minWidth": smallSize //获取小图尺寸 }, //所有上传文件统一为此对象。result.value 不再截取。type 为文件类型,带. beforeSend: function () { itemList.eq(i).find(".progressBox").show(); itemList.eq(i).find(".progress").width("20%"); itemList.eq(i).find(".state").html("正在导入"); // xhr.setRequestHeader("Access-Control-Allow-Origin", "*"); //console.log(1); }, //xhr: function () { //// //console.log("onprogress:" + onprogress); // // //console.log(xhr.upload); // console.log(2); // var xhr = $.ajaxSettings.xhr(); // console.log(3); //// var xhr = new XMLHttpRequest(); //// xhr.setRequestHeader("Access-Control-Allow-Origin", "*"); ////// xhr.setRequestHeader("Access-Control-Allow-Methods", "POST,GET,OPTIONS,DELETE,PUT"); ////// if (xhr.upload) { //检查upload属性是否存在 ////// // //绑定progress事件的回调函数 // //xhr.upload.addEventListener('onprogress', function (e) { // // var percent = (e.loaded / e.total * 100).toFixed(2); // // itemList.eq(i).find(".progress").width(percent + "%"); // // }, false); //// xhr.upload.onprogress = progressFunction; //// } // return xhr; //xhr对象返回给jQuery使用 //}, success: function (data) { status[i].state = 1; var obj = eval("(" + data + ")"); var imgGuid = obj.Data; var filename = file.name.substring(0, file.name.lastIndexOf(".")); if (imgGuid != "" || imgGuid != null || imgGuid != undefined) { var res = dosave(filename, imgGuid, imgHeight, imgWidth); if (res) { itemList.eq(i).find(".state").html("导入成功"); itemList.eq(i).find(".progress").width("100%"); itemList.eq(i).find(".success").addClass("success2"); itemList.eq(i).find(".delete").remove(); } else { itemList.eq(i).find(".state").html("导入失败"); } } else { itemList.eq(i).find(".state").html("上传失败"); } aa++; if (aa == newle) { _this.find(".ok")[0].innerHTML = "上传结束"; _this.find(".ok").css("color", "white"); } //isover = false; //console.log(isover); //if (i == max - 1) { // _this.find(".ok")[0].innerHTML = "上传结束"; // _this.find(".ok").css("color", "white"); //} }, error: function (XMLHttpRequest, textStatus, errorThrown) { parent.ShowMsg('XMLHttpRequest.status: ' + XMLHttpRequest.status); } }); } } function dosave(filename, imgGuid, imgHeight, imgWidth) { var picName = filename; var picRemark = ""; var Guid = ""; var orinalSize = imgWidth + "|" + imgHeight; var index1 = filename.lastIndexOf("_");//正常图片需要去掉流水 var ItemCode = filename.substring(0, index1);//后缀名 var fromPage = GetQueryString("fromPage"); var ajaxUrl = "/PictureMgr/Picture/ImportPicture"; if (fromPage == "HotPicture") { ajaxUrl = "/PictureMgr/HotPicture/ImportHotPicture"; ItemCode = filename;//热点图不存在物料编码+"_01"这种格式,属于:去掉后缀后全匹配 DBS提 } var ajaxData = { imgGuid: imgGuid, picName: picName, picRemark: picRemark, orinalSize: orinalSize, bigSize: bigSize, smallSize: smallSize, ItemCode: ItemCode, CN_S_CLASSGUID: CN_S_Class_Guid, CN_S_FK_CLASSGUID: CN_S_Fk_Class_Guid }; var msg = AjaxManagers(JSON.stringify(ajaxData), ajaxUrl); if (msg != "") { var resultstr = msg; if (resultstr.Success == "true" || resultstr.Success == true) { haveup = true; return true; } else { return false; } } } input.bind("change", function () { if ($(this).val() != "") { _this.openWindow(this.files); } }); }; $(function () { LoadWaterMarkData();//获取水印信息 LoadClassGuid();//获取ITEM对应的类GUID }); var watermarkguid = ""; //获取水印图guid var disType = ""; //获取显示方式 var bigNot = ""; //获取大图是否需要水印 var smallNot = ""; //获取小图是否需要水印 var bigSize = ""; //获取大图尺寸 var smallSize = ""; //获取小图尺寸 function LoadWaterMarkData() { var ajaxUrl = "/PictureMgr/WaterMark/GetMarkImg"; var ajaxData = { CN_S_KEY: "WatermarkSetting" }; var msg = AjaxManagers(JSON.stringify(ajaxData), ajaxUrl); if (msg == "[]") { parent.ShowMsg("加载异常,请先上传水印图!"); return false; } else { msg = msg.replace('[', '').replace(']', ''); var data = eval('(' + msg + ')'); var xm = data.CN_S_VALUE;//获取水印设置 var imgGuid = xm.match(/ID='(\S*)'/)[1];//后台返回水印图路径 // $("#ImgPic").attr('src', imgGuid); watermarkguid = imgGuid.match(/{(\S*)}/)[1]; //获取水印图guid disType = xm.match(/Type='(\S*)'/)[1]; //获取显示方式 bigNot = xm.match(/BigNot='(\S*)'/)[1]; //获取大图是否需要水印 smallNot = xm.match(/SmallNot='(\S*)'/)[1]; //获取小图是否需要水印 bigSize = xm.match(/bigSize='(\S*)'/)[1]; //获取大图尺寸 smallSize = xm.match(/smallSize='(\S*)'/)[1]; //获取小图尺寸 } } var CN_S_Class_Guid = ""; var CN_S_Fk_Class_Guid = ""; //获取ITEM对应的类GUID function LoadClassGuid() { var ajaxUrl = "/ItemArchives/ItemVerson/MagicItemQueto"; var ajaxData = { ITEM: "ITEM" }; var msg = AjaxManagers(JSON.stringify(ajaxData), ajaxUrl); for (var i = 0; i < msg.LinkClassList.length; i++) { if (msg.LinkClassList[i].CN_S_FK_CLASS_NAME == "PICTURE") { CN_S_Class_Guid = msg.LinkClassList[i].CN_S_CLASS_GUID; CN_S_Fk_Class_Guid = msg.LinkClassList[i].CN_S_FK_CLASS_GUID; } } } var haveup = false; function doHaveUp() { return haveup; }