这样一来,解决问题的思路就出来了:
下面用一个Demo展示具体的代码实现用FormData将文件发送到一个php:
<html> <head> <title>Upload</title> <meta charset="utf-8"> </head> <body> <input id="file-input" type="file"> <script> var input = document.getElementById('file-input'); input.onchange = function () { var data = new FormData(); data.append('file', input.files[0]); var request = new XMLHttpRequest(); request.open('POST', './upload.php'); request.send(data); }; </script> </body> </html>