如何在grails中添加对文件类型的验证 [英] How to add validation for file type in grails
问题描述
class Color {
字符串名称
字符串文件位置
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
方法正在读取文件的前几个字节以确定它的文件类型是。所以如果 isFileAllowed
返回false或者文件是空的,会将colorInstance的错误添加到fileLocation属性中。它只会上传文件,如果colorInstance验证成功(以防止文件上传为未保存的对象)。作为一个方面说明,我更喜欢在表中保存文件部分为此原因。它使验证更加笨重,并且不可能在对象和文件之间断开连接。 - 只是我的2c。
def save(){
pre>
$ b $ def colorInstance = new Color(params)
def file = request.getFile(myfile)
if(!file.empty&& uploadService.isFileAllowed(file)){
if(colorInstance.validate ()){
uploadService.uploadFile(file,file.originalName,folderName)
}
}
else {
colorInstance.errors.rejectValue('fileLocation ','error.message.code.here')
}
if(colorInstance.save(flush:true)){
//在这里做任何事
}
else {
render(view:create,model:[coorInstance:colorInstance])
}
}
I have a domain object with following:
class Color { String name String fileLocation static constraints = { name (nullable: false, blank: false) } }
In my controller I'm doing the following:
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]) } }
This all works fine however, I'm not sure how to throw an error when the uploaded file isn't what is allowed. i.e.
uploadService.isFileAllowed(file)
returnsfalse
??How can I throw an error back to the user saying
Uploaded file isn't allowed
when
uploadService.isFileAllowed(file)
returns false ?Note:
The
isFileAllowed
method is reading first few bytes of a file to determine what type of file it is.解决方案So if
isFileAllowed
returns false or the file is empty, it will add an error to the colorInstance to the fileLocation property. It will only upload the file if the colorInstance validates successfully (to prevent files uploaded for unsaved objects).As a side note, I prefer saving files in tables partly for this reason. It makes validation much less clunky and its impossible to have a disconnect between your objects and the files. - Just my 2c.
def save() { def colorInstance = new Color(params) def file = request.getFile("myfile") if (!file.empty && uploadService.isFileAllowed(file)) { if (colorInstance.validate()) { uploadService.uploadFile(file, file.originalName, "folderName") } } else { colorInstance.errors.rejectValue('fileLocation','error.message.code.here') } if (colorInstance.save(flush: true)) { //do whatever here } else { render (view: "create", model: [coorInstance: colorInstance]) } }
这篇关于如何在grails中添加对文件类型的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!