Javascript:TypeError:Value没有实现接口FormData

时间:2013-05-02 20:02:12

标签: javascript jquery

我正在尝试使用FormData通过AJAX将数据发送到PHP脚本。 输入类型文本值似乎没有任何问题,但是当我尝试附加文件时,我得到错误TypeError:Value没有实现接口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) {/*...*/}

});

1 个答案:

答案 0 :(得分:2)

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);
}