Skip to content

Instantly share code, notes, and snippets.

@coderberry
Created October 12, 2010 21:03
Show Gist options
  • Select an option

  • Save coderberry/622918 to your computer and use it in GitHub Desktop.

Select an option

Save coderberry/622918 to your computer and use it in GitHub Desktop.
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