<table id="uploadTable"></table>
<a href="javascript:void(0);" class="input-file">
添加檔案 <input type="file" id="txtFile" multiple>
</a>
.input-file { position: relative; overflow: hidden; display: inline-block; }
.input-file input { position: absolute; top: 0; right: 0; opacity: 0; font-size: 100px; }
$('#txtFile').change(function() {
var files = this.files;
var formData = new FormData();
$.each(files, function(i, file) {
formData.append('files[]', file);
});
$.ajax({
url: '/api/upload',
type: 'POST',
data: formData,
processData: false, // 告訴 jQuery 不要處理資料
contentType: false, // 告訴 jQuery 不要設定 Content-Type
xhr: function() { // 綁定進度事件
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener('progress', function(e) {
if (e.lengthComputable) {
var percent = Math.round(e.loaded / e.total * 100);
updateProgressUI(percent);
}
}, false);
return xhr;
},
success: function(res) { console.log('上傳成功', res); },
error: function() { console.error('上傳失敗'); }
});
});
multer 中間件@RequestParam MultipartFile[] files$_FILES['files']IE10+、所有現代瀏覽器。IE9 以下需 Polyfill 或降級方案。