问题
I am trying to setup a multiple file upload, using FormData html5 api. The problem is that i cannot delete index of an array that is on FormData key. ex:
if(editor.frmData){
editor.frmData.append( 'upload[]', files[0] );
}else{
editor['frmData']=new FormData();
}
This is the code i execute when i select a file. I select more than one file and on the server (php) , $_FILES is array with arrays. ex:
Array
(
[upload] => Array
(
[name] => Array
(
[0] => Screenshot from 2017-02-21 16:04:36.png
[1] => 20170314_124241.jpg
[2] => mob geografica.png
)
[type] => Array
(
[0] => image/png
[1] => image/jpeg
[2] => image/png
)
[tmp_name] => Array
(
[0] => /tmp/phpVQEmFd
[1] => /tmp/phpE5xKUf
[2] => /tmp/php0f4cbi
)
[error] => Array
(
[0] => 0
[1] => 0
[2] => 0
)
[size] => Array
(
[0] => 1088612
[1] => 1324555
[2] => 410839
)
))
My question is how can i delete an entry in formData ex:
editor.frmData.delete('upload[1]');
or
editor.frmData.delete('upload["name"][1]');
Thanks in advance
回答1:
You could use this function to delete one of many values for the same name:
function formDataDelete(frmData, name, index) {
var keep = frmData.getAll(name);
keep.splice(index, 1);
frmData.delete(name);
keep.forEach( value => frmData.append(name, value) );
}
It just deletes the name (and thus all values associated with it), and adds all values again, except the one that was at the indicated index.
回答2:
You can't.
There is set to set a key, and also append which will do the same thing without overwriting the existing key with the same name.
But set has its opposite in delete, append doesn't have an opposite.
The only way to do it is to delete the key and then rewrite all the values you want to keep to it.
来源:https://stackoverflow.com/questions/43372011/formdata-key-as-array