Skip to content

Instantly share code, notes, and snippets.

@masanobuimai
Created February 10, 2009 10:36
Show Gist options
  • Save masanobuimai/61344 to your computer and use it in GitHub Desktop.
Save masanobuimai/61344 to your computer and use it in GitHub Desktop.
Grailsでファイルのアップロード
------ view.gsp ------
<g:form action="save" method="post" enctype="multipart/form-data">
<input type="file" id="fileStore.content" name="fileStore.content"/>
</g:form>
------ Controller.groovy ------
def save = {
def f = request.getFile('fileStore.content')
if (!f.empty) {
def fileContent = new FileContent()
fileContent.image = f.bytes
def fileStore = new FileStore(content:fileContent)
fileStore.contentType = f.contentType
fileStore.filename = f.originalFilename
fileStore.save()
println fileStore.dump()
}
}
-------
class FileStore {
String filename
FileContent content
String contentType
public String toString() { "$id:$filename" }
static mapping = {
content lazy: true
}
}
------
class FileContent {
byte[] image
static def belongsTo = [fileStore: FileStore]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment