Since we need to get the callback information after PHP upload, we studied the asynchronous file upload
Implementation method: use FormData to serialize the storage file, and specify two attributes as false
- contentType
- processData
Attribute explanation:
- Use FormData to serialize files
- Setting contentType: false is to prevent jQuery from processing contentType
- Setting processData: false is to prevent jQuery from processing the data into strings
Sample reference:
html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Upload</title>
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.js"></script>
<script type="text/javascript">
$(function () {
$("button#upload").click(function(event) {
var formdata = new FormData();
formdata.append("file",$('input#test')[0].files[0]);
$.ajax({
url: '自己的上传文件接口',
type: 'POST',
contentType: false,
processData: false,
data: formdata
})
.done(function(data) {
console.log(data);
});
});
});
</script>
</head>
<body>
<input id="test" type="file" />
<button id="upload">点击上传</button>
</body>
</html>