FrancescoDS
FrancescoDS

Reputation: 995

grails: upload files in selected folder

In my Grails application, user needs to upload files in proper folder. I have implemented (as described here) the system that allows to upload documents in a proper folder, but I need to do some modifications. User can upload different kind of files, so I've created folders for each kind of file to upload. So, I need that, after file selection, user can select the folder in which file must be uploaded. How can I do it?

EDIT: I think it is needed some clarification. The folder structure that I need to create is the following:

- Folder
-- Subfolder
--- Sub-subfolder


- base_folder
-- folderOfUser1
---  folderOfKind1
---  folderOfKind2
---  folderOfKindN

-- folderOfUser2
---  folderOfKind1
---  folderOfKind2
---  folderOfKindN

-- folderOfUserN 
---  folderOfKind1
---  folderOfKind2
---  folderOfKindN

So if I cannot use the solution of Sergio because I have "folderOfUserN" that is known only at runtime

Upvotes: 1

Views: 929

Answers (1)

user800014
user800014

Reputation:

Looking at your link, it seems that you need to modify your configuration logic:

environments {
    development {
        uploadFolders = [
          pdf : "c:/temp/upload/{username}/pdf",
          png : "c:/temp/upload/{username}/images",
          gif : "c:/temp/upload/{username}/images",
        ]
    }
    test {
        uploadFolder = [...]
    }
    production {
        uploadFolder = [...]
    }
}

EDIT

When you retrieve the path to the upload replace {username} with the current username. The best place to put this logic is in a service. Assuming that you're using Spring Security:

class UploadService {

  static transactional = false //no need to transactions in this service

  static final String PLACEHOLDER = "{username}"

  def grailsApplication

  def springSecurityService

  String getFolderByTpeAndUser(String type) {
    def user = springSecurityService.currentUser
    def path = config[type]?.replace(PLACEHOLDER, user.username)
    return path
  }

  def getConfig() {
    return grailsApplication.config.uploadFolders
  }

}

Then you need to add one g:select to the view showing this options, and change DocumentController to reflect the option selected by the user.

Upvotes: 3

Related Questions