0

上传一张应为指定尺寸的照片,例如:护照尺寸照片在我的查看页面中。请帮助我。提前谢谢

4

1 回答 1

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 如何管理多部分文件和表单。

在这里,您有一个实用的代码

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

希望能帮助到你!:)

于 2012-07-25T06:24:04.630 回答