Skip to content

Instantly share code, notes, and snippets.

@masanobuimai
Created February 10, 2009 06:17
Show Gist options
  • Save masanobuimai/61269 to your computer and use it in GitHub Desktop.
Save masanobuimai/61269 to your computer and use it in GitHub Desktop.
Grailsでファイルのダウンロード
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]
}
------
import javax.mail.internet.MimeUtility
class FileStoreController {
def show = {
def fileStore = FileStore.get(params.id)
if (request.getHeader('User-Agent') =~ /^MSIE/) {
response.setHeader('Content-disposition',
"attachment; filename=${MimeUtility.encodeWord(fileStore.filename, 'ISO-2022-JP', 'B')}")
} else {
response.setHeader('Content-disposition',
"attachment; filename=${URLEncoder.encode(fileStore.filename, 'UTF-8')}")
}
response.contentType = fileStore.contentType
response.outputStream << fileStore.content.image
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment