为什么不能使用AJAX和formData发送文件?

时间:2018-12-14 18:33:19

标签: javascript php ajax file-upload

这是我的代码,我正在尝试将文件发送到PHP文件,但不能。在JS中,答案是 input_f.files [0] [目标文件] ,但是PHP返回注意:未定义的索引:文件... ,我认为formData不起作用。

PHP代码

<?php
$file = $_FILES['file']['size'];
echo $file;

JS代码

var text = document.getElementById('text');
var input_f = document.getElementById('input_f');//Input file

var xmlhttp = new XMLHttpRequest();
var formData = new FormData();
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        text.innerHTML = this.response;
    }
};
xmlhttp.open("POST","php/convert.php");
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
formData.append('file', input_f.files[0]);
xmlhttp.send(formData);

1 个答案:

答案 0 :(得分:0)

尝试

n
相关问题