帝國CMS 文章內容頁下載附件時還原為原始名稱

在你的內容範本里加入下面的程式碼,可以顯示出原始檔案名稱,下載儲存時也會存成原始檔名:

<!--下載附件時還原為原始檔案名稱-->
<script>document.addEventListener("DOMContentLoaded",()=>{document.querySelectorAll("fieldset").forEach(f=>{let l=f.querySelector("legend"),t=l?.textContent?.trim(),a=f.querySelector("a");if(t&&a){a.textContent=t;a.setAttribute("download",t);l.textContent="下載檔案"}});});</script>

原範本下載後的附件名稱都是一串隨機的長數字,這樣改一下,就不必再手動修改下載後的檔名了,很方便。
(註:後台編輯文章時還是不變的,附件名稱還是一串亂七八糟的數字,發布後前台才會改變)

效果演示:
帝國CMS 文章內容頁下載附件時還原為原始名稱


另一個:

下載模型中,將下載檔案名稱改為與標題一致

在「最終下載頁範本」下方加入以下 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>

文章來源:http://www.artery2000.com/news/2025-09-23/109.html

發表評論

評論 暫無評論

文章目錄