我想通过将文件放在div
元素中来上传文件。我的ajax调用看起来像这样:
$.ajax({
type: "POST",
url: "upload.php",
data: formData,
processData: false,
contentType: false
}).done(function(response){
alert(response);
});
我的upload.php看起来像这样:
$filename = basename($_FILES["file"]["name"]);
$target_path = $directory . basename($_FILES["file"]["name"]);
$target_file = $_FILES["file"]["tmp_name"];
//[some sql here]
move_uploaded_file($target_file, $target_path);
echo $target_path;
将文件拖放到div
元素中会将其上传到服务器,并将数据输入到我的数据库中,但同时也会刷新index.php页面。当我删除move_uploaded_file($target_file, $target_path);
行时,页面不再刷新。期望的结果是文件不会上传到服务器,数据不会输入到数据库中,并且不会刷新index.php。
也转储我的整个upload.js文件,以防万一我错过了明显的事情:
$(document).ready(function(){
$("#dropArea").on("dragover", function(e){
e.preventDefault();
e.stopPropagation();
})
$("#dropArea").on("dragenter", function(e){
e.preventDefault();
e.stopPropagation();
})
$("#dropArea").on("drop", function(e){
e.preventDefault();
e.stopPropagation();
let dataTransfer = e.originalEvent.dataTransfer
let files = dataTransfer.files;
let formData = new FormData();
([...files]).forEach(function(element){
formData.append("file[]", element);
});
$.ajax({
type: "POST",
url: "pages/uploadScript.php",
data: formData,
processData: false,
contentType: false
}).done(function(response){
alert(response);
});
});
});