【问题标题】:Javascript: TypeError: Value does not implement interface FormDataJavascript:TypeError:值未实现接口 FormData
【发布时间】:2013-05-03 04:46:55
【问题描述】:

我正在尝试使用 FormData 通过 AJAX 将数据发送到 PHP 脚本。 输入类型文本值似乎没有任何问题,但是当我尝试附加文件时,我收到错误 TypeError: Value does not implement interface FormData.

我是 FormData 的新手,但我在网上搜索并找不到有关此错误的任何文档。

这是表格:

<form id="item_form" class="item_form" enctype="multipart/form-data">
    <div class="">
        <label for="emp_photos">photos</label>
        <input id="emp_photos" class="inputText" type="file" value="" name="emp_photos">
    </div>
</form>

这里是 Javascript:

var formData = new FormData();      
formData.append('photos', $('#emp_photos').files[0]);

这是我在 firebug 中遇到的错误:

TypeError: Value does not implement interface FormData. 

...igger("ajaxComplete",[N,p]),--b.active||b.event.trigger("ajaxStop")))}return N},...

jquery....min.js (line 5)

我在这里做错了什么?

编辑:ajax 部分

$.ajax({
   type: 'POST',
   url: '"; 
   echo $_SESSION["url_base"];
   echo "operations/add_employes',
   data: formData,
   xhr: function() {  // custom xhr
      myXhr = $.ajaxSettings.xhr();
      if(myXhr.upload) { // check if upload property exists
         myXhr.upload.addEventListener('progress',progressHandlingFunction, false); // for handling the progress of the upload
      }
      return myXhr;
   },
   success: function(msg) {/*...*/}

});

【问题讨论】:

标签: javascript jquery


【解决方案1】:
var inputs = $("input[type=file]"),
    files = [];

// jquery or javascript have a slightly different notation
// it's either accessing functions () or arrays [] depending on which object you're holding at the moment
for (var i = 0; i < inputs.length; i++){
    files.push(inputs.eq(i).prop("files")[0]);
    //files.push(inputs[i].files[0]);
    //filename = inputs[i].files[0].name;
    //filesize = inputs[i].files[0].size;
}

if (formdata) {
    // you can use the array notation of your input's `name` attribute here
    formdata.append("emp_photos[]", files);
}

【讨论】:

  • 非常感谢,非常棒
  • php后端如何访问“emp_photos[]”
  • $_POST['emp_photos'] foreach ($_POST['emp_photos'] as $i => $value) { echo "emp_photos[$i] is $value
    "; }
  • 不应该是$_FILES
  • 可以的。对不起,我的 php 不是最新的。
猜你喜欢
  • 2014-10-12
  • 1970-01-01
  • 2017-08-12
  • 1970-01-01
  • 2018-10-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-24
相关资源
最近更新 更多