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
盧卡,回答你自己的問題呢? –