【发布时间】:2021-05-24 18:08:20
【问题描述】:
我已经到处寻找解决方案了。人们都这样做,但它对我不起作用。
相关的 HTML:
<input type="file" name="file" id="file">
<button type="submit" id="submit" name="import">Import</button>
相关的Jquery:
$(document).ready(function(){
$("button").click(function(){
var fd = new FormData();
fd.append("file", file);
alert(fd.get("file"));
$.ajax({
//URL of the PHP file
url: "readFile.php",
//fd will be send
data:fd,
//json is the type that should be returned
dataType: "json",
//To prevent processing
cache:false,
contentType: false,
processData: false,
//when successful
success: function( data ) {
console.log(data);
}
});
});
});
最后,我编写的用于查找错误的 PHP:(名为 readFile.php)
<?php
if(isset($_FILES["file"]["name"])){
echo json_encode("great");
}else {
echo json_encode("bad");
}
?>
此代码有效,除了 PHP 应该接收文件的部分。
PHP 检查它是否收到了文件。如果它在那里,它会打印出“很好”,但当我点击按钮时,它总是打印出(控制台日志)“坏”。
【问题讨论】:
标签: php jquery ajax file-upload