2012-07-25 1 views

ответ

1

У меня есть это на мой взгляд

<div id="resultAvatar" class="message"> 
    <g:if test="${msg != null}"> 
     ${msg} 
    </g:if> 
    <g:else> 
     <g:message code="profile.avatar.upload.info" /> 
    </g:else> 
</div> 
<div id="warnAvatar" class="warning" style="display:none;"></div> 
<br /> 
<g:form name="avatarForm" id="avatarForm" 
    controller="profile" action="saveAvatar" 
    update="resultAvatar" enctype="multipart/form-data"> 

<style type="text/css"> 
    div.fileinputs { 
     position: relative; 
     width: 70%; 
     text-align: right; 
    } 
    div.fakefile { 
     position: absolute; 
     top: 0px; 
     right: 0px; 
     z-index: 1; 
     width: 148px; 
    } 
    div.fakefile input[type=button] { 
     /* enough width to completely overlap the real hidden file control */ 
     cursor: pointer; 
     width: 148px; 
    } 
    div.fileinputs input.file { 
     position: relative; 
     -moz-opacity:0 ; 
     filter:alpha(opacity: 0); 
     opacity: 0; 
     z-index: 2; 
    } 
</style>   
    <table style="width:100%;"><tr> 
     <td style="pading:40px"> 
      <div class="fileinputs"> 
       <input type="file" class="file" name="avatar" id="avatarBox" style="width:100%"> 
       <div class="fakefile"> 
        <input type="button" value="Selecciona" /> 
       </div> 
      </div> 
     </td> 
     <td> 
      <a href="#" onclick="checkExt();return false;" 
      class="botverde3" 
      >Cargar</a> 
     </td> 
    </tr></table> 

    <%--ti:multiFile id="avatarBox" name="avatar"/--%> 
    <input id="submitAvatar" style="display:none;" type="submit" /> 
</g:form> 
<g:javascript> 

function checkExt(){ 
    var ext = $('#avatarBox').val().split('.').pop().toLowerCase(); 
    if($.inArray(ext, ['gif','png','jpg','jpeg']) == -1) { 
     $('#warnAvatar').html('Extensión de fichero no admitida, solo se permiten archivos .gif, .png, .jpg y .jpeg'); 
     $('#warnAvatar').show(); 
    }else{ 
     var submitBtn = $('#submitAvatar'); 
     submitBtn.click(); 
    } 
} 
</g:javascript> 

А на стороне контроллера

@Secured(['ROLE_USER']) 
def saveAvatar = { 
    def errors 
    def user = theInitService.loggedUser() 
    def hasFiles = fileService.hasFiles(request,'avatar') 

    if(hasFiles) { 
     errors = saveAvatar(request) 
    } 
    if(errors.equals("true")){ 
     render(template:"avatarUploadForm", model:[msg: 'There was a problem, try to upload it later.']) 
    }else{ 
     render(template:"avatarUploadForm", model:[msg: 'The avatar was correctly saved.']) 
    } 
} 

private String saveAvatar(request) { 
    //println("save avatar for bean") 
    def files = fileService.saveAvatarForBean(request) 

    return files 
} 

в saveAvatarForBean подмигнули только Java файл, который вы должны сохранить, есть много документации об этом. Я думаю, что самая разная роль в том, как Grails управляет многостраничным файлом и формой.

Здесь you've получил Pratic кода для выполнения, что

http://ashokabhat.wordpress.com/2012/01/08/save-bytes-image-in-the-application-server-using-java/

Надеется, что это помогает! :)

Смежные вопросы