Created
October 12, 2010 21:03
-
-
Save coderberry/622918 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package berry | |
| import org.springframework.web.multipart.MultipartFile | |
| import org.codehaus.groovy.grails.web.context.ServletContextHolder | |
| class FileUploadService { | |
| boolean transactional = true | |
| def String uploadFile(MultipartFile file, String name, String destinationDirectory) { | |
| def servletContext = ServletContextHolder.servletContext | |
| def storagePath = servletContext.getRealPath(destinationDirectory) | |
| // Create storage path directory if it does not exist | |
| def storagePathDirectory = new File(storagePath) | |
| if (!storagePathDirectory.exists()) { | |
| print "CREATING DIRECTORY ${storagePath}: " | |
| if (storagePathDirectory.mkdirs()) { | |
| println "SUCCESS" | |
| } else { | |
| println "FAILED" | |
| } | |
| } | |
| // Store file | |
| if (!file.isEmpty()) { | |
| file.transferTo(new File("${storagePath}/${name}")) | |
| println "Saved file: ${storagePath}/${name}" | |
| return "${storagePath}/${name}" | |
| } else { | |
| println "File ${file.inspect()} was empty!" | |
| return null | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment