我正在尝试使用Grails应用程序中的jQuery UI Progress栏进行文件上传。我使用g:uploadForm来提交文件,我不知道如何获取XMLHttpRequest对象以获取传输字节流的进度,以便我可以将参数提供给jQuery用于更新自身的UI进度条。这是我到目前为止所尝试但没有运气。我非常感谢任何指导。
$("#progressbar").progressbar({
value: false,
change: function() {
$(".progress-label").text( $("#uploadErrors").progressbar( "value" ) + "%" );
},
complete: function() {
$(".progress-label").text( "Complete!" );
}
});
$(".uploaderForm").submit();
//var fileSize = $("#chseFile")[0].files[0].size;
var request = new XMLHttpRequest();
request.upload.addEventListener('progress', function(event) {
var percent = event.loaded / event.total;
console.log(percent);
$("#progressbar").progressbar( "value", percent * 100 );
});
控制器的上传方法非常简单,因为它只是将工作传递给服务:
def upload() {
params.selectedBatch = selectedBatch
// Diag
println "*** Request info: " + request
println "*** Session info: " + request.getSession()
println "*** Servlet context: " + request.getSession().getServletContext()
def f = request.getFile('file')
if (f.empty) {
println "File cannot be empty!"
}
else {
// The case that we have an Excel file upload. This if statement might need to be
// a switch statement in the future when we start accepting other upload formats like
// CSV and/or XML.
if (params.fileTypegrp.toInteger() == 1) {
// We know its an Excel file, now we use a switch for the data type.
switch (params.dataTypegrp.toInteger()) {
case 1:
fileImportService.excelAccountFileUpload(params)
println "upload complete!"
break
.
.
.
答案 0 :(得分:0)
UploadController.groovy
import grails.web.JSONBuilder
def uploadFile = {
if (request instanceof MultipartHttpServletRequest) {
for (filename in request.fileNames) {
def uploadedMessage = StringUtils.EMPTY
MultipartFile file = request.getFile(filename)
JSONBuilder jSON = new JSONBuilder()
JSON json = jSON.build {
name = file.originalFilename
size = file.size
}
results = json.toString()
}
}
render results
}
Upload.gsp
<input id="fileupload" type="file" name="files[]" multiple>
<div id="progress" class="progress">
<div class="progress-bar"></div>
</div>
<script>
jQuery('#fileupload').fileupload({
url: 'uploadFile',
dataType: 'json',
progressall: function (e, data) {
var progress = parseInt(data.loaded / data.total * 100, 10);
jQuery('#progress .progress-bar').css(
'width',
progress + '%'
);
}
});
</script>
我没有完全尝试过此代码,但这应该可行,如果您收到任何错误,请添加评论。我假设你已经添加了所有必要的JS和CSS。
仅供参考:我正在使用:http://blueimp.github.io/jQuery-File-Upload/