0
我有几个文件上传,我想显示上传百分比。 在javascript中我有这样的代码:上传MultipartFile百分比
function uploadFunction(fileType){
//CSRF attribute for spring security
var token = $("meta[name='_csrf']").attr("content");
var header = $("meta[name='_csrf_header']").attr("content");
var formData = new FormData();
var fileControl = document.getElementById(fileType);
var length = fileControl.files.length;
for(var i = 0; i< length; i++){
formData.append('file[]',fileControl.files[i]);
}
formData.append('idFleet', selectedFleet);
formData.append('idCar', $("#selectedCar").val());
if(fileType!='dat')
formData.append('initialKm', 0);
else
formData.append('initialKm', $("#initialKm").val());
return jQuery.ajax({
url : 'upload',
type: 'POST',
contentType: false,
processData: false,
data:formData,
beforeSend:function(xhr) {
xhr.setRequestHeader(header, token);
waitingModal.showPleaseWait();
},
success: function(data,status,xhr){
//No exception occurred
if (data.status){
//Also the field are right(for e.g. form value)
if(data.success){
//Store information if file is datatable
if (fileType=='datatable')
$("#datatablePath").val(data.result[0]);
notifyMessage(fileType + " has been uploaded!", 'success');
uploadResult=true;
}
else{
//code if there are some error into form for example
}
} else {
notifyMessage(data.result, 'error');
$('#acquisitionWizard').bootstrapWizard('show',2);//show
uploadResult=false;
}
},
error: function(xhr,status,e){
window.location.href = "/DART/500";
}
}).complete(function() {
//add timeout because otherwise user could see a too fast waiting modal
setTimeout(function(){
waitingModal.hidePleaseWait();
}, 1000);
});
}
,并在春天我有
@Override
@RequestMapping(value = { "/upload"}, method = RequestMethod.POST)
public @ResponseBody Response uploadFiles(Principal principal, @RequestParam("file[]") MultipartFile[] file, @RequestParam("idFleet") Integer idFleet, @RequestParam("idCar") Integer idCar, @RequestParam("initialKm") Integer initialKm) {
try {
ArrayList<String> path=fleetAcquisitionServices.uploadFiles(principal.getName(), file, idFleet, idCar, initialKm);
return new Response(true,true,path,null);
} catch (FileEmptyException e) {
....
}
}
,并使用MultipartFile
transferTo
方法我上传的文件。 我看了一些指南,但都是servelet,是否有可能在我的代码中集成百分比进度? 我使用此代码从客户端的网页和REST调用,并与此方法我没有收到关于内存的错误。
我已经使用“onprogress”事件来显示信息。这里有一个链接,说明如何使用它与jQuery:[jQuery ajax进展](http://www.dave-bond.com/blog/2010/01/JQuery-ajax-progress-HMTL5/) – Roberto
谢谢它的作品, – luca
卢卡,回答你自己的问题呢? –