How to remove value from FormData

跟風遠走 提交于 2019-11-27 17:43:59

问题


Here is a way to append file to FormData :

  var data = new FormData();
  jQuery.each($('#file')[0].files, function(i, file) {
          data.append('file-'+i, file);
  });

is it possible to do as below ?

     data[i].remove();???
 or  data[i] = file;??

how iIcan remove or modify a value from data


回答1:


You cannot do anything other than append items to a FormData object. See the Spec. It would be better if you use a dictionary/object to store all the values you want to add/modify before you actually construct the object.

var data = {};
jQuery.each($('#file')[0].files, function(i, file) {
  data['file-'+i] = file;
});

//modify the object however you want to here

var formData = new FormData();
for (var key in data) {
  formData.append(key, data[key]);
}



回答2:


I know this thread is old but I just found this : https://developer.mozilla.org/en-US/docs/Web/API/FormData/delete

I'm sur it could help. You can use formData.delete(name) to delete the entry of formData with "name" key.



来源:https://stackoverflow.com/questions/21443553/how-to-remove-value-from-formdata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!