[分享]下载模型中,将下载文件名改为与标题一致
在“最终下载页模板”下方加入以下JS,可实现下载文件时存档文件名与标题一致,自动加入文件后辍,省了让那一长串随机数字带来的烦恼: <!--将下载文件名改为与标题一致--> <script>document.addEventListener("DOMContentLoaded",function(){var t=(document.title.split(" - ")[0]||"download").trim(),e={"application/zip":".zip","application/x-rar-compressed":".rar","application/x-7z-compressed":".7z","application/gzip":".gz","application/x-tar":".tar","application/pdf":".pdf","application/msword":".doc","application/vnd.openxmlformats-officedocument.wordprocessingml.document":".docx","application/vnd.ms-excel":".xls","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":".xlsx","application/vnd.ms-powerpoint":".ppt","application/vnd.openxmlformats-officedocument.presentationml.presentation":".pptx","application/octet-stream":"","text/plain":".txt","image/jpeg":".jpg","image/png":".png","image/gif":".gif","image/webp":".webp","video/mp4":".mp4","audio/mpeg":".mp3","audio/wav":".wav","video/x-msvideo":".avi","application/x-msdownload":".exe"},n=document.querySelectorAll('a[href*="doaction.php?enews=DownSoft"]');for(var r=0;r<n.length;r++)!function(n){n.addEventListener("click",function(r){r.preventDefault();var a=n.href,o=new XMLHttpRequest;o.open("GET",a,!0),o.responseType="blob",o.onload=function(){if(200===o.status){var n=o.getResponseHeader("Content-Type")||"",r=e[n.toLowerCase()]||function(t){var e={0:".zip",1:".rar",2:".pdf",3:".docx",4:".7z",5:".tar",6:".xlsx",7:".pptx",8:".mp4",9:".mp3",10:".txt"};try{var n=t.match(/pathid=(\d+)/);return n?e[n[1]]||"":""}catch(t){return""}}(a)||".bin",i=t+r,c=o.response;if(window.navigator&&window.navigator.msSaveOrOpenBlob)window.navigator.msSaveOrOpenBlob(c,i);else{var d=URL.createObjectURL(c),l=document.createElement("a");l.href=d,l.download=i,document.body.appendChild(l),l.click(),document.body.removeChild(l),URL.revokeObjectURL(d)}}else alert("下载失败,请稍后再试。")},o.onerror=function(){alert("请求出错,下载失败。")},o.send()})}(n[r])});</script> 适用于前台用户投稿中上传的文件,后台管理员指定链接的下载地址虽然也可以改名字,但意义不大。
上传以下图片:

|
[该贴被修改 2 次,最后修改时间 2025-05-29 09:16:43 ]
_/_/_/_/ 我的签名就是这么不一样 ^_* _/_/_/_/ 在下的小站:ARTERY.cn
|