3

我知道关于这个问题有几个问题,我已经梳理了其中的许多问题,但还没有找到正确实现目标的正确方法。本质上,我试图捕获表单中的多个文件输入并将它们推送到 PHP。我至少弄清楚了 PHP 部分,但它的 Jquery 我正在苦苦挣扎。我通过调试注意到的主要问题之一是我的操作参数根本没有通过。我发布的代码如下。任何帮助将不胜感激。谢谢。

$(document).ready(function () {
  //if submitting imgages form
  $('#submit').click(function () {
    // match anything not a [ or ]
    regexp = /^[^[\]]+/;
    var fileInput = $('.putImages input[type="file"]');
    var fileInputName = regexp.exec(fileInput.attr('name'));

    // make files available
    var data2 = new FormData(fileInput);
    jQuery.each($(fileInput)[0].files, function (i, file) {
      data2.append(fileInputName + '[' + i + ']', file);
    });

    //organize data
    var data = new FormData($('input[name^="uploadFile"]'));
    jQuery.each($('input[name^="uploadFile"]')[0].files, function (i, file) {
      data.append(i, file);
    });

    $.ajax({
      type: 'GET',
      url: 'productadminupdate.php',
      data: data2 + "&action=" + 'images',
      cache: false,
      contentType: false,
      processData: false,
      success: function (response) {
        $('#imgform_result').html(response).fadeIn();
      }
    });

    return false;
  });
});
4

1 回答 1

1

我不知道我是否明白了,但是当人们想到 ajax 上传器时,他们并不真正知道这一点。实际上,不是 JavaScript 做的。该技术存在于将表单的操作定位到 Iframe。这个 iframe 应该被隐藏这里是一个例子:

<form method='post' enctype='multipart/form-data' action='YOUR_FILE.php' target='upload_iframe'>        
    <label>photo1:</label>
    <input type='file' name='photo1'/>
    <label>photo2:</label>
    <input type='file' name='photo2'/>
    <label>photo3:</label>
    <input type='file' name='photo3'/>
    <label>photo4:</label>
    <input type='file' name='photo4'/>
    <label>photo5:</label>
    <input type='file' name='photo5'/>
    <input type='submit' value='Send'/>
    </form>
</div>
<iframe name='upload_iframe'></iframe>

因此,在 iframe 中,您的 php 代码将进行上传,使用 $_FILES 获取信息。

于 2012-09-02T03:05:21.690 回答