Hosted File links in portal - dynamically generate?

Posted by gwf on 12-May-2016 11:59

I would like to have links to hosted files in a portal, with different hosted files associated with different records. I started by creating a string field with integration name 'hosted_file_id' to hold the hosted file ID for each record, but nothing I've tried either server-side or client-side has worked to get the link to display properly.

For example, a formula field which can be moved into a section through the Rollbase drag-and-drop UI:

function main() {
    return '<p><a href="{!#HOSTED_FILE.' + {!hosted_file_id#value} + '}">File Link</a></p>'; 
}

main();

Or, a script component to generate the link:

<div>
    <p id='fileLink'></p>
</div>

<script>
    
var hostedFileId = '{!hosted_file_id#value}';
var hostedFileToken = '{!#HOSTED_FILE.' + hostedFileId + '#url}';

document.getElementById('fileLink').innerHTML = '<a href="' + hostedFileToken + '">File Link</a>';

</script>

However, this formula field did display the link properly, but the hosted file ID is hard-coded and so it can't change for different records:

function main() {
    return '<p><a href="{!#HOSTED_FILE.123456}">File Link</a></p>'; 
}

main();

Any tips on how to do this?

Thanks,

Greg

Posted by Shiva Duriseati on 16-May-2016 01:21

Hi Greg,

Hosted file token is URL which is constructed from server side and displayed as token. So the value (ex: {!#HOSTED_FILE.123456}) cannot be modified on the client side. The variable  var hostedFile=' {!#HOSTED_FILE.' + hostedFileId + '#url}'; will simply return a string that cannot be parsed to URL.

However,along with the hostedFileID ,if you were able to get fileName(tmp file name assigned by Rollbase),contentType and suggestedName(actual file name) , then using these parameters you can construct the URL as below:

var hostedFileURL="http://"+"{!#HOST_NAME}"+"/storage/servlet/Image?c="+"{!#CURR_CUSTM.id}"+"&fileName="+fName+"&contentType="+cType+"&suggestedName="+sName;

After modification your script should like this:

<div>

   <p id='fileLink'></p>

</div>

<script>

//Assuming your storage server name is storage .It can be checked in components.xml

var hostedFileURL ="http://"+"{!#HOST_NAME}"+"/storage/servlet/Image?c="+"{!#CURR_CUSTM.id}"+"&fileName="+fName+"&contentType="+cType+"&suggestedName="+sName;

document.getElementById('fileLink').innerHTML = '<a href="' + hostedFileURL + '">File Link</a>';

</script>

PS:  I tested by giving hardcoded values to above parameters. Please let me know whether you were able to get those parameters and construct the URL.

Regards,

Shiva

All Replies

Posted by Shiva Duriseati on 16-May-2016 01:21

Hi Greg,

Hosted file token is URL which is constructed from server side and displayed as token. So the value (ex: {!#HOSTED_FILE.123456}) cannot be modified on the client side. The variable  var hostedFile=' {!#HOSTED_FILE.' + hostedFileId + '#url}'; will simply return a string that cannot be parsed to URL.

However,along with the hostedFileID ,if you were able to get fileName(tmp file name assigned by Rollbase),contentType and suggestedName(actual file name) , then using these parameters you can construct the URL as below:

var hostedFileURL="http://"+"{!#HOST_NAME}"+"/storage/servlet/Image?c="+"{!#CURR_CUSTM.id}"+"&fileName="+fName+"&contentType="+cType+"&suggestedName="+sName;

After modification your script should like this:

<div>

   <p id='fileLink'></p>

</div>

<script>

//Assuming your storage server name is storage .It can be checked in components.xml

var hostedFileURL ="http://"+"{!#HOST_NAME}"+"/storage/servlet/Image?c="+"{!#CURR_CUSTM.id}"+"&fileName="+fName+"&contentType="+cType+"&suggestedName="+sName;

document.getElementById('fileLink').innerHTML = '<a href="' + hostedFileURL + '">File Link</a>';

</script>

PS:  I tested by giving hardcoded values to above parameters. Please let me know whether you were able to get those parameters and construct the URL.

Regards,

Shiva

Posted by gwf on 17-May-2016 12:10

That worked. Thank you very much, Shiva!

Greg

Posted by Shiva Duriseati on 18-May-2016 01:07

Glad it worked, you mentioned in the description that you are holding hosted_file_id for each record,could you please let us know how you are doing it I am bit curious?

Posted by gwf on 26-May-2016 09:36

Sure, we have audio files which are too big for the file upload field (which BTW works great for Word and other documents). Thus I needed a workaround for files above 2MB to associate each file with a specific record, rather than a more standard use of hosted files being application- or portal-wide.

Thanks again!

Greg

This thread is closed