I don’t know how ajax uploads multiple images and sends them to the background.
For a single image, you can use base64 to send it to the background. However, if you use multiple images, this method is not suitable because the base64 will be very large.
How is it sent to the background? If the background is PHP, how does it receive it?
According to the comment method, I obviously chose 2 pictures to upload. Why is only 1 file displayed in the background?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<script src="//cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<form id="form">
<input type="file" multiple="" name="file">
<button id="btn" type="button">上传</button>
</form>
<script>
document.getElementById('btn').onclick = () => {
$.ajax({
url: './test.php',
type: 'POST',
dataType: 'text',
data: new FormData(document.getElementById('form')),
processData: false,
contentType: false,
})
.done(function(data) {
console.log(data);
})
}
</script>
</body>
</html>
It’s better to use a form. The reason why you choose direct ajax is probably because you don’t want to refresh the page.
In this case, you can use formdata for ajax submission. This is a new attribute of H5. You can see the example for details. Its main function is to The content in the form field is encapsulated into formdata and then submitted using ajax. The name of the form control corresponds to the background parameter name. As for multiple pictures, just use a set of inputs with the same name. Below are the js and renderings. If you still don’t understand, you can ask.
http://www.jianshu.com/p/756e...
Multiple pictures are just append to FormData
Just use FormData to submit
https://developer.mozilla.org...
The main thing is to append the file to be uploaded to formData
How to get it on the backend (php: inside $_FILES)
It seems that the original intention of the questioner was that sharing it all together would be too big? You can pass them one by one, and you can try again if you fail, so the implementation cost is low.