我有以下的域對象:如何添加驗證文件類型的Grails
class Color {
String name
String fileLocation
static constraints = {
name (nullable: false, blank: false)
}
}
在我的控制,我做了以下內容:
def save() {
def colorInstance = new Color(params)
if (colorInstance.save(flush: true)) {
def file = request.getFile("myfile")
if (!file.empty && uploadService.isFileAllowed(file)) {
uploadService.uploadFile(file, file.originalName, "folderName")
}
}
else {
render (view: "create", model: [coorInstance: colorInstance])
}
}
然而,這一切工作正常,當上傳的文件不是允許的時候,我不知道如何拋出錯誤。即uploadService.isFileAllowed(file)
返回false
??
我如何拋出一個錯誤返回給用戶說
上傳的文件是不允許
時uploadService.isFileAllowed(file)
返回false?
注:
的isFileAllowed
方法首先讀取文件的幾個字節,以確定它是什麼類型的文件。
真的取決於顏色和文件之間的關係。詳細解釋「uploadService.uploadFile」的做法 – 2013-03-10 20:02:02
該方法將文件保存在文件夾中並返回保存文件的絕對路徑。該路徑保存在'fileLocation'屬性中 – birdy 2013-03-10 21:09:53