FormData键作为数组 [英] FormData key as array

查看:218
本文介绍了FormData键作为数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用FormData html5 API设置多个文件上传. 问题是我无法删除FormData键上的数组的索引. 例如:

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

这是我选择文件时执行的代码.我选择了多个文件,并且在服务器(php)上,$ _FILES是带有数组的数组. 例如:

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

我的问题是如何删除formData ex中的条目:

My question is how can i delete an entry in formData ex:

editor.frmData.delete('upload[1]');

editor.frmData.delete('upload["name"][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.

这篇关于FormData键作为数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆