Lightning imposing alohaRedirect on links in Visualforce

visualforce

I have a very simple text file download link embedded in a Visualforce page that has lightningStylesheets="true". Here is the link code:

<a style="text-decoration:none" href="data:text/plain;charset=utf-8;base64,testtext" download="Filename.txt">Export</a>

This works exactly as expected in Salesforce classic. However LEx has a very annoying quirk where it prepends an alohaRedirect to what is otherwise clearly not a web address… here is the resulting link address:

https://na142.visual.force.com/one/one.app#/alohaRedirect:text/plain;charset=utf-8;base64,testtext

This opens up a blank tab as a result, and doesn't actually download the file.

This is obviously standard behavior intended to redirect Salesforce URLs to be compatible with the LEx framework. But, it makes no sense in this case as it's not a web URL, let alone a SF URL.

Any idea how to disable this behavior or otherwise work around this quirk? Any help is greatly appreciated!

UPDATE:

One other thing I've tried is migrating the hyperlink tag to a javascript function, and instead used a commandButton that calls this javascript function in the onclick event:

function download (filename,body) 
{
    var element = document.createElement('a');
    element.setAttribute('href', 'data:text/plain;charset=utf-8;base64,' + body);
    element.setAttribute('target', '_self');
    element.setAttribute('download', filename);
    element.style.display = 'none';
    document.body.appendChild(element);
    element.click();
    document.body.removeChild(element);
}

Unfortunately this behaves exactly the same way: works flawlessly in Classic, but in LEx the link is prepended with the aloha redirect.

Best Answer

Figured it out, and thank goodness it was very simple. Apparently using target="_blank" in the link solves this problem outright. The prepending behavior must be associated with the "_self" target value, which is the default for html hyperlinks.

Related Topic