FormData
MDN FormDataopen in new window
<div>
<p>姓名 <input type="text" id="name"></p>
<p>附件 <input type="file" id="attachment" multiple></p>
<p>
<button type="button" id="btn">提交</button>
</p>
</div>
<script src="jquery-3.3.1.js"></script>
<script>
$('#btn').on('click', function () {
const data = new FormData()
const files = $('#attachment')[0].files
data.append('name', $('#name').val())
for (let i = 0; i < files.length; i++) {
data.append('attachment', files[i], files[i].name)
}
console.log(data)
console.log(data.get('name'))
console.log(data.get('attachment'))
console.log(data.getAll('attachment'))
$.ajax({
url: '/url',
type: 'post',
processData: false,
contentType: false,
data: data,
success: function () {
alert('ok')
}
})
})
</script>