Joget DX 8 Stable Released
The stable release for Joget DX 8 is now available, with a focus on UX and Governance.
Hi Thang,
I had created a similar beanshell to copy an uploaded file and then rename it.
Hope you'd find it useful!
import org.joget.apps.form.service.FileUtil; import org.joget.commons.util.SetupManager; import java.lang.String; import org.springframework.context.ApplicationContext; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import java.io.File; //This method creates a copy of the file uploaded via File Upload form element in Joget and appends the name of the person who uploaded the file. This works whether you've used the default base path under System Settings or if you've changed the default base path for the file upload. //Here, I'm using the destination directory structure as <FILE_UPLOAD_BASE_PATH>/filesWithRequesters/app_formuploads/<TABLE_NAME>/<CURRENT_PROCESSID>/.. against the default Joget file storage path as : <FILE_UPLOAD_BASE_PATH>/app_formuploads/<TABLE_NAME>/<CURRENT_PROCESSID>/.. //Please replace 'fileUpload' everywhere with the name of the table the form is bound to. //Please replace 'requester' in Line 33 with the fieldID of the form field which contains the name of the user whose username has to be appended with the file. //Please replace 'file' in '#form.fileUpload.file#' with the fieldID of the file upload form field. //Please change the below destinationDirectory variable according to your need. public void updateFileName(){ try{ String sourceDirectory=""; ApplicationContext ac = FileUtil.getApplicationContext(); String formUploadPath = SetupManager.getBaseDirectory(); SetupManager setupManager = (SetupManager) ac.getBean("setupManager"); String dataFileBasePath = setupManager.getSettingValue("dataFileBasePath"); if(dataFileBasePath!=null && dataFileBasePath.length() > 0){ sourceDirectory=dataFileBasePath; } else{ sourceDirectory=formUploadPath; } String destinationDirectory = sourceDirectory+File.separator+"filesWithRequesters"+File.separator+"app_formuploads"+File.separator+"fileUpload"+File.separator+"#assignment.processId#"+File.separator; sourceDirectory+=File.separator+"app_formuploads"+File.separator+"fileUpload"+File.separator+"#assignment.processId#"+File.separator; String sourceFilePath = sourceDirectory+"#form.fileUpload.file#"; String fileExtension = FilenameUtils.getExtension(sourceFilePath); String destinationFilePath = FilenameUtils.removeExtension(destinationDirectory+"#form.fileUpload.file#"); destinationFilePath += "_"+"#form.fileUpload.requester#."+fileExtension; File sourceFile = new File(sourceDirectory+"#form.fileUpload.file#"); File destinationFile = new File(destinationFilePath); FileUtils.copyFile(sourceFile,destinationFile); } catch(Exception e){ System.out.println("Some error occurred in renaming the file. "+e); } } return updateFileName();
Peace!
Thanks for the tip. However, Can we attach this into the form as well? let say we have 2 fields: 1. fileupload 2. get the name of fileupload Once user upload there file abc.pdf then the 2 form will get the file name = abc Can we do that?
If the fields you say are in the same form, you can always use a JS to get the name and remove the extension. Otherwise if you want the name of the uploaded file (without the extension) in some other form, you can use a BeanShell (similar to the one above) as a tool before this form and pre-fill the database field of the form with the renamed value. Hope this helps!
Yes!1. They are in the same form. But I dont know how to write JS to get it. Could you advise?
Hi Adelina,
If I understand correctly, you want to be able to change the name of the file that you upload in a form. Right?
I don't think you could do that using a JavaScript but yes, I think I could help you create a BeanShell tool for that purpose.
Please let me know if this is what you intend to do.
Thanks!
HI Ashutosh Tripathi, Please help me to do it in bean shell. I have uploaded the file (example abc.pdf). In the datalist, I just want to view file name is abc only. Pls help Thank you
Hi Ashutosh Tripathi yes its my concern please help me out in this scenario, I want to rename a file before its uploaded on harddisk... the basic purpose of this is... if the filename is in arabic or any other language its sotred on harddisk with question marks Regards,
Raising the "abc.pdf" file through a form.
I can javascript through automatically change the name as "abc_v1.1.pdf".
And so. Any help is greatly appreciated.