Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/456.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过javascript和post-send获取ajax文件_Javascript_Ajax - Fatal编程技术网

如何通过javascript和post-send获取ajax文件

如何通过javascript和post-send获取ajax文件,javascript,ajax,Javascript,Ajax,我有这个html格式的表单用于多文件上传 <form class="userform" method="post" action="upload.php" role="form" enctype="multipart/form-data" > <input name="title" type="text" ><br/> <input type="file" name="media[]" > &

我有这个html格式的表单用于多文件上传

<form class="userform" method="post" action="upload.php" role="form"   enctype="multipart/form-data" >
        <input  name="title" type="text" ><br/>
        <input  type="file" name="media[]" >
        <div class="filesupload">
          <input type="file" name="media[]" >
        </div>
<script>
 $(document).on("click",".add-new-file",function()
            {
                $('.filesupload').append('<input  name="media[]" type="file"  >');
            });
</script>
</form>


$(文档)。在(“单击“,”。添加新文件”上,函数() { $('.filesupload').append(''); });
我将通过javascript获取输入文件并发送到upload.php进行上传。但是我不知道如何在javascript中获取文件值。

如果您使用HTML5

   var fileList = document.getElementById("yourInput").files;
    for(var i = 0; i < fileList.length; i++) {
        //Do something with individual files
    }
var fileList=document.getElementById(“yourInput”).files;
对于(var i=0;i
使用jquery

$("input[name=file1]").change(function() {
    var names = [];
    for (var i = 0; i < $(this).get(0).files.length; ++i) {
        names.push($(this).get(0).files[i].name);
    }
    $("input[name=file]").val(names);
});​
$(“输入[name=file1]”)。更改(函数(){
变量名称=[];
对于(var i=0;i<$(this).get(0).files.length;++i){
name.push($(this.get(0).files[i].name);
}
$(“输入[名称=文件]”).val(名称);
});​

您所说的“文件值”是什么意思?您想要文件名、大小或内容吗?好的!我将通过upload.php中的$_files[“media”]获取文件,但我不知道如何通过$(ajax)发送文件。您可能需要先阅读这篇文章。而且,只有支持xhr2的浏览器才有可能