我正在尝试使用 jquery ajax 和 php 将文本文件上传到我的服务器这是我的 jquery 代码:
//This is inside inner method
var file = new Blob([text],{type: 'text/plain'});
var KysForm = document.querySelector("form");
var formData = new FormData(KysForm);
formData.append("Assets",file,"assets.txt");
var request = new XMLHttpRequest();
$.ajax({
url: 'kys_editor.php',
data: formData,
processData: false,
contentType: false,
type: 'POST',
success: function(data){
alert(data);
}
});
这是我的 php 代码:
<?php
include "kys_DbConnect.php";
$data="";
if($_SERVER["REQUEST_METHOD"] === "POST"){
$data = $_POST["data"];
$target_dir = "Images/";
$target_file = $target_dir . basename($_FILES["Assets"]["name"]);
$uploadOk = 1;
if (move_uploaded_file($_FILES["Assets"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["Assets"]["name"]). " has been uploaded.".$_FILES["Assets"]["tmp_name"];
} else {
echo "Sorry, there was an error uploading your file.";
}
}
if(isset($data)){
$author = "Jois";
$insert = $con->prepare("INSERT INTO kys_write (Author, writing) VALUES (?,?)");
$insert->bind_param("ss",$author,$data);
$insert->execute();
echo "Successfully inserted".$data;
}
?>
这是我的 html 代码:
<form method="post" action="kys_editor.php" enctype="multipart/form-data">
<input type="text" id="write" name="data" >
<input type="file" accept="image/*" name="image" onchange="showImage()" id="image">
<input type="submit" value="Submit" name="submit" onclick="inner()">
</form>
我不知道错误是什么,但我无法得到回复。我的意思是从未调用成功方法。我能知道我哪里出错吗?