1
0
-1

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.

    CommentAdd your comment...

    2 answers

    1.  
      2
      1
      0

      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!

      1. thang

        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?

      2. Ashutosh Tripathi

        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!

      3. thang

        Yes!1. They are in the same form. But I dont know how to write JS to get it. Could you advise?

      CommentAdd your comment...
    2.  
      1
      0
      -1

      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!

      1. thang

        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

      2. Farhan

        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,

      CommentAdd your comment...