var SPECIAL_CHARACTERS = [['"', '%22'], ['#', '%23'], ['%', '%25'], ['&', '%26'], ['(', '%28'], [')', '%29'], ['+', '%2B'], [',', '%2C'], [';', '%3B'], ['<', '%3C'], ['=', '%3D'], ['>', '%3E'], ['?', '%3F'], ['@', '%40'], ['\\', '%5C'], ['|', '%7C']]; function mailValid() { //alert(downloadmail) $("#downloadmailerrortip").html(""); var errortip = ""; //^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$ //^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$ var reg = new RegExp("^[a-z0-9A-Z]+([._\\-]*[a-z0-9A-Z])*@([a-zA-Z0-9]+[-a-z0-9A-Z]*[a-z0-9A-Z]+.){1,63}[a-z0-9A-Z]+$"); //正则表达式 var emailvalue = $("#downloadmail").val(); //alert(emailvalue) if (emailvalue) { if (!reg.test(emailvalue)) { //正则验证不通过,格式不对 errortip = "邮箱格式不正确!"; } } else { errortip = "请输入您的邮箱!"; } if (errortip) { $("#downloadmailerrortip").html(errortip); return false; } else { return true; //return false; } } function downLoadWinClose() { //$("#downloadtomailwin").css("display","none"); $('#dialogBg5').fadeOut(300, function () { $('#dialog5').addClass('bounceOutUp').fadeOut(); }); } /** * */ function downLoadWinShow(thisp, fileUrl, fileName) { className = $(thisp).attr('class'); $('#dialogBg5').fadeIn(300); $('#dialog5').removeAttr('class').addClass('animated ' + className + '').fadeIn(); //$("#downloadtomailwin").css("display",""); $("#downloadfileurl").val(fileUrl); $("#downloadfilename").val(fileName); } function downLoadLinkToMail() { var downloadfileurl = $("#downloadfileurl").val(); var fileName = $("#downloadfilename").val(); var mailCheck = mailValid(); if (mailCheck) { var emailvalue = $("#downloadmail").val(); submitLinkToMail(emailvalue, downloadfileurl, fileName); } } function submitLinkToMail(emailvalue, downloadfileurl, fileName) { $("#downLoadLinkToMailbtn").attr('disabled', true); $.ajax({ type: "post", url: "/sendFileToMail.jspx", data: { emailvalue: emailvalue, fileName: fileName, fileUrl: downloadfileurl }, dataType: "json", success: function (data) { //$("#downLoadLinkToMailbtn").attr('disabled',"false"); $('#downLoadLinkToMailbtn').removeAttr("disabled"); if (data.success == '1') { alert("发送成功!"); downLoadWinClose(); } else { alert("发送失败!"); } }, failure: function () { //$("#downLoadLinkToMailbtn").attr('disabled',"false"); $('#downLoadLinkToMailbtn').removeAttr("disabled"); alert("发送失败,请联系管理员!"); } }); } function downLoadByName(attachName, attachPath, atype) { //attachPath = "/u/cms/www/111.pdf" $.ajax({ type: 'POST', url: "/updateDownLoadCount.jspx", data: { attachName: attachName, attachPath: attachPath, atype: atype }, success: function (resp) { } }); if (attachPath && attachPath.indexOf("http") < 0) { attachName = encodeURIComponent(attachName.trim()); if (atype == 1) { if (attachPath.indexOf(".pdf") >= 0 || attachPath.indexOf(".PDF") >= 0) { window.open("/downloadByName.jspx?isPreview=Y&filePath=" + attachPath + "&fileName=" + attachName + "&t=" + new Date()); } else { window.open(attachPath); } } else { //下载 window.open("/downloadByName.jspx?filePath=" + attachPath + "&fileName=" + attachName + "&t=" + new Date()); } } else { console.log("执行接口"); if (atype == 1) { if (attachPath.indexOf(".pdf") >= 0 || attachPath.indexOf(".PDF") >= 0) { $.ajax({ type: 'POST', url: "/previewOrDownload.jspx", data: { filePath: attachPath, isPreview: "Y", }, success: function (resp) { console.log("filepath",attachPath); const result = attachPath.replace(/&#40;/g, "(").replace(/&#41;/g, ")").replace(/&/g, "%26").replace(/\+/g, "%2B"); console.log("result", result); console.log("resp", resp); window.open(encodeURI("pdfjs/web/viewer.html?file="+result)); } }); } else { if (attachName.includes("zip") || attachName.includes("rar") || attachName.includes("mp4")|| attachName.includes("stp")) { window.open(attachPath); } else { downLoadUrl(attachName, attachPath); } } } else { if (attachName.includes("zip") || attachName.includes("rar") || attachName.includes("mp4")|| attachName.includes("stp")) { window.open(attachPath); } else { downLoadUrl(attachName, attachPath); } } } } function downLoadUrl(fileName, url) { if(url) { for(var i = 0; i < SPECIAL_CHARACTERS.length; i++) { var characterArr = SPECIAL_CHARACTERS[i]; //特殊字符 var specialStr = characterArr[0]; if(url.indexOf(specialStr) > -1) { //需要替换的字符 var replaceStr = characterArr[1]; url = url.split(specialStr).join(replaceStr); } } fetch(url).then(response => response.blob()).then(blob => { downloadBlobFile(fileName, new Blob([blob], { type: 'application/octet-stream' })) }) } else { alert('没有需要下载的文件!'); } } function downloadBlobFile(fileName, blob) { if (typeof window.navigator.msSaveBlob !== 'undefined') { // 兼容IE,window.navigator.msSaveBlob:以本地方式保存文件 window.navigator.msSaveBlob(blob, decodeURI(fileName)) } else { // 创建新的URL并指向File对象或者Blob对象的地址 const blobURL = window.URL.createObjectURL(blob) // 创建a标签,用于跳转至下载链接 const tempLink = document.createElement('a') tempLink.style.display = 'none' tempLink.href = blobURL tempLink.setAttribute('download', decodeURI(fileName)) // 兼容:某些浏览器不支持HTML5的download属性 if (typeof tempLink.download === 'undefined') { tempLink.setAttribute('target', '_blank') } // 挂载a标签 document.body.appendChild(tempLink) tempLink.click() document.body.removeChild(tempLink) // 释放blob URL地址 window.URL.revokeObjectURL(blobURL) } }