【发布时间】:2014-06-14 00:00:51
【问题描述】:
我正在尝试在 Grails 应用程序中使用 jQuery UI 进度条进行文件上传。我正在使用 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
.
.
.
【问题讨论】:
-
我知道这并不能真正回答您的问题,但您是否考虑过使用文件上传插件之一,例如uploadr?
-
我没有调查过。我确实看到了一些插件,但它们似乎有点旧,所以我不确定它们是否仍然适用,但我会调查一下。谢谢。
-
您也可以在这里粘贴您的控制器代码吗?我之前使用 GRAILS 完成了 jquery 进度条,但没有使用 g:uploadForm。
-
@LalitAgarwal 我添加了相关的控制器代码。任何建议高度赞赏。感谢您对我的问题感兴趣。
-
@AnonymousHuman 抱歉,我错过了您的编辑并添加了我的编辑方式。请看我的回答并尝试一下。