我是grails框架的新手,我遇到了以下问题:
我无法弄清楚我将如何做到这一点..我看过grails aynch编程,但我无法弄清楚如何实现它。
如果有人指导这个
,我将非常感激答案 0 :(得分:0)
我曾在一个表格上工作过,其中包括文件上传,我希望这是异步完成的。它花了很多谷歌搜索,不幸的是我不记得我使用的任何参考文献,但这是我最终做的。
我在文件输入中包含了一个onchange属性,当选择文件时,会调用javascript函数来验证并异步上传文件。
文件输入:
<input type="file" name="uploadMedical" id="uploadMedical" onchange="validateAndUpload(this, $(this));" accept=".pdf,.jpg,.jpeg" multiple>
JavaScript (我尽力删除与您的问题无关的代码,同时仍然留下足以帮助解决您的问题):
function validateAndUpload(input, documentInput){
var uploadForm = $("#uploadForm");
// Stop the browser from submitting the form.
uploadForm.submit(function(event) {
event.preventDefault();
});
// Get the selected files from the input.
var files = input.files;
// Create a new FormData object.
var formData = new FormData();
// Loop through each of the selected files.
for (var ii = 0; ii < files.length; ii++) {
var file = files[ii];
// Add the file to the request.
formData.append('supportingDocumentation', file, file.name);
}
//Include ID of record to associate this upload with
formData.append('id', getAppealId());
// Submit the form using AJAX.
$.ajax({
type: 'POST',
dataType : "html",
url: url_str,
data: formData,
processData: false,
contentType: false,
beforeSend:function(){
//indicate that the file is uploading
},
success:function(htmlData){
//display the new list of files on the page
},
error:function(ee){
//indicate that the upload failed
},
complete:function(){
//reset the file input so more files can be uploaded
}
})
}