about crud with attachment I am not able to do the save action

0

You're adof to run an actio in Grails. The next step is for the action to save an attachment and a title to the attachment.

Does anyone provide me with crud links with attachments?

    
asked by anonymous 11.01.2016 / 20:54

1 answer

1

Aline,

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

Class mark, 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.

    
12.01.2016 / 15:05