如何通过 javascript 和 post 发送 ajax 获取文件

How can I get files by javascript and post send for 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[]" >
        <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>

我将在 javascript 之前获取输入文件并发送到 upload.php 进行上传。但我不知道如何获取 javascript.

中的文件值

如果您使用 HTML5

   var fileList = document.getElementById("yourInput").files;
    for(var i = 0; i < fileList.length; i++) {
        //Do something with individual files
    }

使用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);
});​