File upload
<g:uploadForm>
lo único que hace es agregar el atributo enctype="multipart/form-data"
al formulario.
GSP:
Preview:
<g:if test="${uploadedFileName}">
<a href="${grailsApplication.config.apache.docroot.location + "/" + uploadedFileName}" target="_blank">
<img src="${grailsApplication.config.apache.docroot.location + "/" + uploadedFileName}"
class="img-thumbnail" width="200" height="230"/>
</a>
</g:if>
Link to image:
<g:if test="${uploadedFileName}">
<a href="${grailsApplication.config.apache.docroot.location + "/" + uploadedFileName}" target="_blank"><i
class="fa fa-file-photo-o fa-2x"></i></a>
</g:if>
<g:uploadForm action="formSubmit">
<input type="file" name="myFile" />
<g:submitButton name="submit1" value="enviar"/>
</g:uploadForm>
Controller:
import org.springframework.security.access.annotation.Secured
import org.springframework.web.multipart.commons.CommonsMultipartFile
import us.incorpora.sigrem.utils.Htdocs
@Secured(['permitAll'])
class TestController {
def index() {
}
def formSubmit() {
CommonsMultipartFile multipartFile = request.getFile('myFile')
if (multipartFile.isEmpty()) {
flash.error = "Debe elegir un archivo"
redirect action: "index"
return
}
Htdocs htdocs = new Htdocs(dir: grailsApplication.config.sigrem.uploads.location)
File file = htdocs.transferMultipartFile(multipartFile, "base_filename")
render view: "index", model: [uploadedFileName: file.name]
}
}
Si se desa utilizar el plugin bootstrap-fileinput
, solo hay que modificar <input type="file" name="myFile" />
por <t:fileinput name="myFile"/>
y agregar en el head
<t:assetsFileinput/>
.