about crud with attachment I’m not managing to make the save action

Asked

Viewed 65 times

0

You’re using it to run a Grail action. It is the following that the action needs to save an attachment and a title to the attachment.

Someone gives me links of Ruds with attachments?

1 answer

1

Aline,

Unfortunately I cannot share the link, but I put the codes right here. You are using dynamic Scaffold views:

Class tag, omitted package:

class Marca {

  String nome
  byte[] fotografia

  static constraints = {
    nome unique: true
    fotografia nullable: true, maxSize: 1024 * 1024 * 2
  }

}

Brand Controller:

class MarcaController {

  static scaffold = true

  static allowedMethods = [save: "POST", update: "POST", delete: "DELETE", mostrarFoto: "GET"]

  def mostrarFoto (Marca marcaInstance) {
    if(marcaInstance?.fotografia){
        def byteArray = marcaInstance.fotografia

        response.setHeader("Content-disposition", "attachment; filename=fotografiaMarca${marcaInstance.id}.jpeg")
        response.setHeader("Content-Length", "${byteArray.length}")
        response.contentType = "application/octet-stream"

        response.outputStream << byteArray
    }
    else{
        '*'{ render status: NO_CONTENT }
    }
  }

  @Transactional
  def save(Marca marcaInstance) {
    if (marcaInstance == null) {
        notFound()
        return
    }

    if (marcaInstance.hasErrors()) {
        respond marcaInstance.errors, view:'create'
        return
    }

    marcaInstance.save flush:true

    request.withFormat {
        form {
            flash.message = message(code: 'default.created.message', args: [message(code: 'marcaInstance.label', default: 'Marca'), marcaInstance.id])
            redirect marcaInstance
        }
        '*' { respond marcaInstance, [status: CREATED] }
    }
}

Any questions, post here.

  • Hi! What is this? '*'{ render status: NO_CONTENT }

  • Aline, with the command render status: NO_CONTENT, you are informing Grails that it is to give an answer with the status NO_CONTENT, whose http code is 204. That is, if someone makes an ajax call for example, and there is no photograph for the tag, Grails returns that status to the person. On this page (https://en.wikipedia.org/wiki/List_of_HTTP_status_codes) you can see the HTTP status codes and their meanings. Already the *{ } command means to return this status, regardless of whether it was an ajax call, or whether it was the browser that called the show action.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.