AnsweredAssumed Answered

Using HTML5 download attribute to append file version to file name on download.

Question asked by kayatao on Apr 30, 2016
Eventually I found the way and want to share to others who might face the same issue. Steps are listed below.
1.In file “C:\Alfresco\tomcat\webapps\share\components\documentlibrary\actions-min.js”, delete the below line.
<javascript>
ah={downloadUrl:I(Alfresco.constants.PROXY_URI,ai)+'?a=true" target="_blank',
</javascript>

Then add these lines.  They are used to modify file name in document action section.
<javascript>
vs = aj.jsNode.properties.versionLabel, //Get file version
fname = aj.jsNode.properties.name, //Get file name
extension = fname.substring(fname.lastIndexOf('.'),fname.length); //Get only file extension
fname1 = fname.replace(extension, " (Version " + vs + ")" + extension); //Append file name with version and extension
ah={downloadUrl:I(Alfresco.constants.PROXY_URI,ai)+'" download="'+fname1+'" target="_blank', //Add HTML5 download attribute and set its value to the modified file name
</javascript>

2. In file “C:\Alfresco\tomcat\webapps\share\components\document-details\document-versions-min.js”, delete the below line.
<javascript>
var t=Alfresco.constants.PROXY_URI+"api/node/content/"+s.nodeRef.replace(":/","")+"/"+s.name+"?a=true",r="";
</javascript>

Then add this line.  It is used to modify file name in download history section.
<javascript>
var t=Alfresco.constants.PROXY_URI+"api/node/content/"+s.nodeRef.replace(":/","")+"/"+s.name,r=""; //Remove direct download parameter, ?a=true, from URL
</javascript>
   
Then delete the below line.
<javascript>
r+='      <a href="'+t+'" class="download" title="'+this.msg("label.download")+'"> </a>';
</javascript>

And add these lines instead.
<javascript>
var extension = s.name.substring(s.name.lastIndexOf('.'),s.name.length); //Get only file extension
var fname = s.name.replace(extension, " (Version " + s.label + ")" + extension); ); //Append file name with version and extension
r+='      <a href="'+t+'" class="download" title="'+this.msg("label.download")+'" download="'+fname+'"> </a>'; //Add HTML5 download attribute and set its value to the modified file name
</javascript>

Outcomes