gpt4 book ai didi

javascript - AJAX上传文件不起作用

转载 作者:行者123 更新时间:2023-11-28 06:01:50 26 4
gpt4 key购买 nike

我想使用 ajax 在浏览器上上传文本文件,但我不知道如何做到这一点。文件未上传,var_dump($files) 回显 array(0)。

HTML:

<form id="uploadsql" action="upload.js" method="post" enctype="multipart/form-data">
<input id="uploadFile" type="text" disabled="disabled" placeholder="Choose File" />
<div class="fileUpload btn btn-primary">
<span>Upload</span>
<input id="uploadBtn" name="file" type="file" class="upload"required />
</div>

<div class="fileUpload btn btn-primary">
<span>Submit</span>
<input type = "submit" id="uploadBtn" class="upload" /></div>

</form>
<h4 id='loading' >Loading...</h4>
<div id="message"></div>
<script type="text/javascript">
document.getElementById("uploadBtn").onchange = function () {
document.getElementById("uploadFile").value = this.value;
};
</script>

JS:

$(document).ready(function (e) {
$("#uploadsql").on('submit',(function(e) {
e.preventDefault();
$("#message").empty();
$('#loading').show();
$.ajax({
url: "upload.php", // Url to which the request is send
type: "POST", // Type of request to be send, called as method
cache: false, // To unable request pages to be cached
success: function(data) // A function to be called if request succeeds
{
$('#loading').hide();
$("#message").html(data);
}
});
}));


});

PHP:

<?php
set_time_limit(0);
header('content-type: text/html; charset=utf-8');
header("access-control-allow-origin: *");

include 'connect.php';


if(isset($_FILES["uploadBtn"]["type"]))
{
echo 'yes';

} else {
$files = $_FILES;
var_dump ($files);
}



?>

我在这里做错了什么?提前致谢!

最佳答案

通过查看您的 ajax 调用,您没有将“data”属性包含到 ajax 对象中。但我建议你看看这个答案 jQuery AJAX file upload PHP

关于javascript - AJAX上传文件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37213941/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com