jquery - 用grails和jquery上传文件

标签 jquery grails

我有一个包含多个输入内容的表单以及一个doc或pdf文件上传。

//Multiple input fields are here then upload ->
<div class="controls">
    <div class="fileupload ${ person?.attachment ? 'fileupload-exists' : 'fileupload-new' }" id="attachment" data-provides="fileupload" data-name="attachment">
        <span class="btn btn-file">
            <span class="fileupload-new">Add</span>
            <span class="fileupload-exists">Change</span>
            <input type="file" />
        </span>
        <span class="fileupload-preview"></span>
        <a href="#" class="close fileupload-exists" data-dismiss="fileupload" style="float: none">×</a>
    </div>
</div>

并使用jquery提交表单
$(document).on('click', "#submit", function () {
    //TODO Take the chosen file and upload it with form data
    //Taking data from form and contructing a object and posting it with ajax
}

当按#submit时,我不知道如何使用表单上传doc / pdf文件。

客户端上传来自here

我看过g:formg:uploadForm,但我真的不想使用它们,因为它们似乎可以刷新页面和/或重定向用户。

最佳答案

使用以下功能在表单提交时使用ajax发送文件
在查看结束时

        function formSubmit(){

           var formData=new FormData($('form#create-form')[0]);
            $.ajax({url: 'createAttachment', type:'POST', data: formData, processData: false,contentType: false,dataType: 'script',success:function(result){

            }});
            return false
        }

在 Controller 端,使用以下代码访问文件对象
def createAttachment = {
    List attachmentsFiles=[]
    request.fileNames.each {
        def singleFile = request.getFile(it)
        if(singleFile.getOriginalFilename()) {
            attachmentsFiles.add(singleFile)
        }
    }
    attachmentsFiles.each { file ->
       println(file.getOriginalFilename())
    }
  //put your code here
}

附件文件将具有所有上传的文件。

关于jquery - 用grails和jquery上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30910083/

相关文章:

javascript - ajax 请求每秒返回 10 个响应

jquery - 将 form 标签内的所有控件移动到 div

javascript - 在 javascript 中使用 $.each 和 new Image() 时顺序不一致

grails - Controller 单元测试中的模拟gorm方法

grails - Grails 站点网格中的条件检查

javascript - 防止 Adob​​e Edge preload.js 文件加载 jquery

javascript - 使用省略号 chop 文本,始终将插入符号保留在末尾

string - 增加VARCHAR的限制

grails - 如何在grails Controller 的hasErrors()方法中省略字段验证

grails - Grails 中的渲染和返回渲染有什么区别