We a requirement to change the file name while downloading. we using “window.open(documentUrl,‘_blank’);” in script block to download the file. documentUrl will be something like “https://hostname/download/15082709015510867de0383b6a4d02ae/”. This url we are getting from CSP where the documents are storing. Please suggest, is there any means to change filename using script or java(CAF side).
When loading that CSP link directly from the browser, you would have no control over the filename that the target server sends back. That request doesn’t flow through your portlet, it appears to go directly to the CSP server?
Perhaps you can change your code to use a CAF Command Link that acts as a proxy for that target URL. If the request goes though the CAF action, then you can control what filename is sent back.
For example, the java code for the action handler that you bind to the Command Link action property may look something like this:
public String downloadLink_action() {
try {
//TODO: get the url you want to download here.
String url = "https://hostname/download/15082709015510867de0383b6a4d02ae/";
boolean forceDownload = false; //false to allow the browser to display the content
URLContentExportBean exportBean = new URLContentExportBean(url, forceDownload) {
/**
* Override to change the filename that is sent to the client
*/
@Override
public getExportFileName() {
return "my_custom_filename_here";
}
};
streamFileDataToResponse(exportBean);
} catch (Exception e) {
error(e);
}
return null;
}
For your reference, below are the links to the 9.8 javadocs used in the above example:
Thanks for your reply. The thing is we are using the script “window.open(documentUrl,‘_blank’);” where documentUrl is the url we are getting it from the CSP. So we cant use command Link control as such. Any other suggestion please?
As I said before, it is impossible for the client-side code to change the filename that the server sends back.
If it is a firm requirement to modify the returned filename, then you must change the documentUrl that you are loading. I already provided an example of one way to do that.
Just to complete the Java code, I don’t know if things have changed in 9.9 but you need to use a URL (and getExportFileName needs a return type, of course):
public String downloadLink_action() {
try
{
URL url = new URL("https://hostname/download/15082709015510867de0383b6a4d02ae/");
boolean forceDownload = true; //false to allow the browser to display the content
URLContentExportBean exportBean = new URLContentExportBean(url, forceDownload) {
/**
* Override to change the filename that is sent to the client
* @return
*/
@Override
public String getExportFileName() {
return "my_custom_filename_here";
}
};
streamFileDataToResponse(exportBean);
} catch (Exception e) {
error(e);
}
return null;
}
I also set forceDownload to true, as that seemed to be closer to the original problem.
One last thing - while this works fine for a Command Link/Button/whatever, it does render the page unusable. Is there any way to send the file and leave the page still working?