简体   繁体   English

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

[英]How can I get files by javascript and post send for ajax

I have this form in html for multiple file uploading . 我在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>

I will get input files by javascript and send to upload.php for uploading. 我将通过javascript获取输入文件并发送到upload.php进行上传。 but I don't know how to get files values in javascript. 但我不知道如何在javascript中获取文件值。

If you using HTML5 如果您使用HTML5

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

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

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

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