Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation 如何在grails中添加文件类型验证_Validation_Grails_File Upload_Groovy - Fatal编程技术网

Validation 如何在grails中添加文件类型验证

Validation 如何在grails中添加文件类型验证,validation,grails,file-upload,groovy,Validation,Grails,File Upload,Groovy,我有一个具有以下内容的域对象: 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 = r

我有一个具有以下内容的域对象:

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])
  }
}
在控制器中,我执行以下操作:

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])
  }
}
这一切都很好,但我不知道如何抛出一个错误时,上传的文件是不允许的。i、 e.
uploadService.isFileAllowed(file)
返回
false

我如何向用户返回一个错误

不允许上载文件

uploadService.isFileAllowed(file)
返回false时

注:


isFileAllowed
方法读取文件的前几个字节,以确定文件的类型

如果将错误消息保存到闪存中,然后在页面上呈现(如果存在)该怎么办


在控制器中应用此登录

String fileName = "something.ext";
        int a = fileName.lastIndexOf(".");
        String extName = fileName.substring(a);
        System.out.println(fileName.substring(a));
        ArrayList<String> extList = new ArrayList<String>();
        extList.add("jpg");
        extList.add("jpeg");
        extList.add("png");
        if(extList.contains(extName))
        {
            System.out.println("proceed");
        }
        else{
            System.out.println("throw exception");
        }
String fileName=“something.ext”;
int a=fileName.lastIndexOf(“.”);
字符串extName=fileName.substring(a);
System.out.println(fileName.substring(a));
ArrayList extList=新的ArrayList();
extList.add(“jpg”);
添加(“jpeg”);
添加(“png”);
if(extList.contains(extName))
{
系统输出打印项次(“继续”);
}
否则{
System.out.println(“抛出异常”);
}

因此,如果
isFileAllowed
返回false或文件为空,它将向fileLocation属性的colorInstance添加一个错误。只有当colorInstance成功验证时,它才会上载文件(以防止为未保存的对象上载文件)

作为旁注,我更喜欢将文件保存在表中,部分原因是这样的。它使验证更加简单,并且不可能在对象和文件之间断开连接。-就我的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])
  }
}

真正取决于颜色和文件之间的关系。详细解释uploadService.uploadFile的作用该方法将文件保存在文件夹中并返回保存文件的绝对路径。该路径保存在
fileLocation
propertyFlash消息中,这是一个很好的建议。然而,为了在不知道Grails中的“flass message magic”的情况下抛出错误,birdy可以简单地使用包含错误的新模型渲染以前的视图。这也是问题标题的答案,但不是问题的答案。