doujingjiao0015 2014-06-20 15:26 采纳率: 0%
浏览 21

我找不到我的javascript-ajax-php“上传文件”代码有什么问题

this is my first question here (usually I like to find my own ways to solve problems) but I just can't find problem in my file upload code. It's supposed to use AJAX. I simplified everything so it would be easier for you to read. Here is HTML form:

<form id="fileForm" enctype="multipart/form-data" method="POST" action="php/uploadfile.php">
<p>Insert file: <input type="file" id="fileUp" name="fileUp" />
<button type="submit" id="uploadButton" onclick="sendFile();">Upload</button></p>
</form>

Now here goes javascript sendFile() function:

function sendFile()
{    
    var forma = document.getElementById("fileForm");
    var failas = document.getElementById("fileUp");
    var uploadButton = document.getElementById("uploadButton");

    forma.onsubmit = function(event)
    {
        event.preventDefault();
    }

    uploadButton.innerHTML = "Uploading, please wait!";

    var newFile = failas.files[0];

    var formData = new FormData(forma);
    formData.append("fileUp", newFile, newFile.name);

    alert(newFile.name);// Here it says filename.jpg it means everything is ok at this stage

    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            alert (xmlhttp.responseText);
        }
    }

    xmlhttp.open("POST", "php/uploadfile.php", true);
    xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");  
    xmlhttp.send(formData);
}

And php:

<?php
echo var_dump($_FILES);
?>

It should alert contents of $_FILES, but it says array(0){} even if I try $_REQUEST. So, if anyone could suggest what may be wrong, it would be appreciated :]

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向