简体   繁体   English

HTML5 + jQuery AJAX上传-没有文件发送到服务器

[英]HTML5 + jQuery AJAX upload - No file sent to server

I'm trying to implement a HTML5 ajax file upload with the help of HTML5's File API. 我正在尝试借助HTML5的File API来实现HTML5 ajax文件上传。 It is based on Afzaal Ahmad Zeeshan's answer to this question. 它基于Afzaal Ahmad Zeeshan对这个问题的回答。

My main aim would be to be able to let users upload .xls and .xlsx spreadsheet for later use. 我的主要目标是让用户上载.xls.xlsx电子表格以供以后使用。

HTML 的HTML

<form class="form-uploadXLS" method="post" action="php/uploadXLS.php" enctype="multipart/form-data">
    <div class="form-group">
        <div class="col-md-12">
            <input type="file" name="xls" class="xls" />
        </div>
    </div>
    <input type="button" value="Upload" class="btn-uploadXLS" />
</form>
<progress></progress>

And here are the jQuery event handlers, just like in the above mentioned answer: 这是jQuery事件处理程序,就像上面提到的答案一样:

File input onChange event: 文件输入onChange事件:

$('.xls').on('change', function () {
    var file = this.files[0];
    var fileName = file.name;
    var fileType = file.type;
    var fileSize = file.size;

    console.log("file name: " + fileName + ", type: " + fileType + ", size: " + fileSize);
});

This works correctly, the file's name, type and size gets logged to the server. 这可以正常工作,文件的名称,类型和大小将记录到服务器。

Upload button's onClick event: 上载按钮的onClick事件:

$('.btn-uploadXLS').on('click', function (event) {
    event.preventDefault();
    console.log("Upload button clicked");
    var formData = new FormData($('.form-uploadXLS')[0]);

    $.ajax({
        url: 'php/uploadXLS.php', //Server script to process data
        type: 'POST',
        xhr: function () { // Custom XMLHttpRequest
            var myXhr = $.ajaxSettings.xhr();
            if (myXhr.upload) { // Check if upload property exists
                myXhr.upload.addEventListener('progress', progressHandlingFunction, false); // For handling the progress of the upload
            }
            return myXhr;
        },
        //Ajax events
        beforeSend: function (stuff) {
            console.log("BeforeSend");
            console.log(stuff);
        },
        success: function (data) {
            console.log("Success!");
            console.log(data);
        },
        error: function (error) {
            console.log("Error!");
            console.log(error);
        },
        // Form data
        data: formData,
        //Options to tell jQuery not to process data or worry about content-type.
        cache: false,
        contentType: false,
        processData: false
    });
});

On server side, I'm using this PHP code to check if the file has been sent to the server 在服务器端,我正在使用此PHP代码检查文件是否已发送到服务器

if(!empty($_FILES['xls'])) {
    echo '<pre>',print_r($_FILES,1),'</pre>';
}
else {
    die('no $_FILES variable');
}

And here's the result of print_r: 这是print_r的结果:

Array
(
[xls] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)

)

According to the documentation , error code 4 means: UPLOAD_ERR_NO_FILE Value: 4; No file was uploaded. 根据文档 ,错误代码4表示: UPLOAD_ERR_NO_FILE Value: 4; No file was uploaded. UPLOAD_ERR_NO_FILE Value: 4; No file was uploaded.

What am I doing wrong? 我究竟做错了什么?

Edit: 编辑:

If I disable the click event listener and submit the form as normal, the file gets uploaded without problems. 如果我禁用了单击事件侦听器并按正常方式提交表单,那么文件上传将没有问题。

I've noticed, that the formData variable doesn't have any value. 我注意到, formData变量没有任何值。 This is the value of formData after it's been set: FormData { append=append()} 设置后,这是formData的值: FormData { append=append()}

For some reason the problem was with the formData variable, previously defined as such: 由于某种原因,问题出在formData变量上,该变量先前定义为:

var formData = new FormData($('.form-uploadXLS')[0]);

I've changed the index to 1 like this.. 我已经将索引更改为1。

var formData = new FormData($('.form-uploadXLS')[1]);

.. and for some reason it works now. ..由于某种原因,它现在可以工作了。

Instead of: 代替:

if(!empty($_FILES['xls'])) {

try this: 尝试这个:

if(!empty($_FILES['xls']['name'])) {

type = $_FILES['xls']['type']) tmp = $_FILES['xls']['tmp_name']) 类型= $_FILES['xls']['type']) tmp = $_FILES['xls']['tmp_name'])

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

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