简体   繁体   English

XMLHttpRequest-发送文件?

[英]XMLHttpRequest - sending files?

On my forms submission I have: 在提交表单时,我有:

var formData = new FormData();

        for (var i = 0; i < ctx.files.length; i++) {
            formData.append('file[]', ctx.files[i]);

        }

On my server side I just dump the $_POST. 在服务器端,我只是转储$ _POST。

I get: 我得到:

array(1) {
 ["file"]=>
  array(1) {
    [0]=>
   string(17) "[object FileList]"
 }
}

It's coming through as a string, how can I get it as an array of files? 它以字符串形式出现,如何将其作为文件数组来获取?

Here's the whole request: 这是整个请求:

var formData = new FormData();

        for (var i = 0; i < ctx.files.length; i++) {
            formData.append('file[]', ctx.files[i]);
            //formData.push(ctx.files[i]);
        }

        // now post a new XHR request
        var xhr = new XMLHttpRequest();
        xhr.open('POST', '/site-manager-gateway/add-content');
        xhr.onload = function () {
            if (xhr.status === 200) {
                console.log('all done: ' + xhr.status);
            } else {
                console.log('Something went terribly wrong...');
            }
        };

        xhr.send(formData);

Missing things 缺少东西

Add this before send: 发送前添加:
xhr.setRequestHeader("Content-Type","multipart/form-data");

Accessing the files 访问文件

After that, the files can be access like this: 之后,可以像这样访问文件:

$count = count($_FILES['file']['name']);
for($i=0; $i<$count; $i++)
{
   echo 'Uploaded File With FileName: '.$_FILES['file']['name'][$i].'<br/>';

}

More Info: $_FILES variable 更多信息:$ _FILES变量

http://www.php.net/manual/en/reserved.variables.files.php http://www.php.net/manual/zh/reserved.variables.files.php

If you go to this link you will see that the $_FILES variable will be like this: 如果转到此链接,您将看到$_FILES变量将如下所示:

array(1) {
    ["upload"]=>array(5) {
        ["name"]=>array(3) {
            [0]=>string(9)"file0.txt"
            [1]=>string(9)"file1.txt"
            [2]=>string(9)"file2.txt"
        }
        ["type"]=>array(3) {
            [0]=>string(10)"text/plain"
            [1]=>string(10)"text/plain"
            [2]=>string(10)"text/plain"
        }
        ["tmp_name"]=>array(3) {
            [0]=>string(14)"/tmp/blablabla"
            [1]=>string(14)"/tmp/phpyzZxta"
            [2]=>string(14)"/tmp/phpn3nopO"
        }
        ["error"]=>array(3) {
            [0]=>int(0)
            [1]=>int(0)
            [2]=>int(0)
        }
        ["size"]=>array(3) {
            [0]=>int(0)
            [1]=>int(0)
            [2]=>int(0)
        }
    }
}

"But...where are my files??" “但是……我的文件在哪里?”

Your files are located in a temporally folder (In linux, the default is /tmp). 您的文件位于临时文件夹中(在Linux中,默认值为/ tmp)。
The only way to recover your files from this temporally folder is using on each file the php function: 从此临时文件夹恢复文件的唯一方法是在每个文件上使用php函数:

move_uploaded_file

Which according to the php documentation : 根据PHP文档

"This function checks to ensure that the file designated by filename is a valid upload file (meaning that it was uploaded via PHP's HTTP POST upload mechanism). If the file is valid, it will be moved to the filename given by destination. “此功能将检查以确保filename所指定的文件是有效的上传文件(意味着该文件是通过PHP的HTTP POST上传机制上传的。)如果该文件有效,它将被移至destination指定的文件名。

This sort of check is especially important if there is any chance that anything done with uploaded files could reveal their contents to the user, or even to other users on the same system." 如果上传的文件所做的任何事情都可能向用户甚至同一系统上的其他用户透露其内容,则这种检查尤其重要。”

move_uploaded_file Usage move_uploaded_file的用法

This is how you should do it. 这就是你应该怎么做。 If you uploads directory is called "uploads" then: 如果您上载的目录称为“上载”,则:

<?php
$uploads_dir = '/uploads';
foreach ($_FILES["file"]["error"] as $key => $error) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["file"]["tmp_name"][$key];
        $name = $_FILES["file"]["name"][$key];
        move_uploaded_file($tmp_name, "$uploads_dir/$name");
    }
}
?>

And that will save all your files in the "/uploads" directory. 然后,将所有文件保存在“ / uploads”目录中。

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

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