2010年10月28日木曜日

Grailsでファイルアップロード

こんなドメインクラスを作ってファイルをアップロードする。


class UploadFile {

String name
Long size
String contentType
byte[] bytes

static constraints = {
}
}


これだと一覧表示でUploadFile.list()すると、不要にファイルの内容まで引っ張ってきてしまって遅い。なのでこうする。


class UploadFile {

String name
Long size
String contentType
UploadData data

static constraints = {
}
}

class UploadData {

byte[] bytes

static belongsTo = [UploadFile]

static constraints = {
}
}


これだとUploadDataへの関連がデフォルトでlazy:trueなので、一覧表示が速くなる。DataBindingはできなくなるけど仕方ない。他にいい方法ないかなぁ。