3

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

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Sorry downvote trincot . This is a magnificent solution
How can i rename name['name'][index]
Can you give an example of what you mean? You want to rename upload[] to something_else for a given index, so it is no longer part of the "array" of values for upload[]? NB: new questions should better be posted as such.
In question , i have on server the upload[] and upload['name' ,' type' ...] . upload['name'] is also an array. As I need to rename the files i need to change upload['name'][index]
So you want $_FILES["upload"]["name"][0] = "somenewfilename";?
|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.