简体   繁体   English

AJAX上传文件不起作用

[英]AJAX uploading files not working

I want to upload text files on browser using ajax but I'm not getting how to do that. 我想使用ajax在浏览器上上传文本文件,但我不知道如何做到这一点。 The file is not uploaded and var_dump($files) echoes array(0). 文件未上传,var_dump($ files)回显数组(0)。

HTML: 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: 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:

<?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);
}



?>

What am I doing wrong here? 我在这做错了什么? Thanks in advance! 提前致谢!

well by looking at your ajax call, you are not including the "data" attrribute to the ajax object. 通过查看您的ajax调用,您不会将“数据”包含在ajax对象中。 But i recommend you look at this answer jQuery AJAX file upload PHP 但我建议你看看这个答案jQuery AJAX文件上传PHP

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM