【问题标题】:Image Uploading with FormData multiple inputs使用 FormData 多个输入的图像上传
【发布时间】:2017-09-12 21:11:40
【问题描述】:

如何将 FormData 与多个文件输入一起使用?

<input type="file" id="file1">

<input type="file" id="file2">

<input type="file" id="file3">

【问题讨论】:

标签: php ajax upload form-data


【解决方案1】:

现在html5 支持在一个输入文件中上传多个文件!如果您想在不点击的情况下开始自动上传,您应该通过onchange 方法进行。在 ajax 中,您必须使用 new FormData()

上传.php

<input type="file" id="files" name="filefield" multiple="multiple">
<script type="text/javascript">
$("#files").on("change",function(){
    var ajaxData = new FormData();
    var obj = $(this)[0];
    $.each(obj.files,function(i,file){
        ajaxData.append("file['"+i+"']",file);
    });
    $.ajax({
        url :'index.php',
        data: ajaxData,
        contentType: false,
        processData: false,
        dataType: 'json',
        type:"POST",
        success : function() {

        }
    });
})

index.php

<?php
  var_dump($_FILES);
?>

【讨论】:

    猜你喜欢
    • 2019-10-24
    • 2015-12-22
    • 2018-02-20
    • 1970-01-01
    • 1970-01-01
    • 2015-04-23
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    相关资源
    最近更新 更多