How do I send a file input message in javascript?

I wrote this program to help me send post request using javascript

only those items with a dispatch class will be sent

I am storing all data in a variable because this can be reused in another ajax function.

and then use it to create a form and submit it.

this can send any data on the page, not just an input element

but it can't send the file because I don't know how to store the file input element to a variable and then create the input file element for that file.

Is it possible for my program to process the input file?

function send(url)
{
    var data=getData();
    var form=document.createElement('form');
    form.setAttribute('method', 'POST');
    form.setAttribute('action', url);
    for(x in data)
    {
        var hidden=document.createElement('input');
        hidden.setAttribute('type', 'hidden');
        hidden.setAttribute('name', x);
        hidden.setAttribute('value', data[x]);
        form.appendChild(hidden);
    }
    document.body.appendChild(form);
    form.submit();
}

function getData()
{
    var data={};
    var sendNode = document.getElementsByClassName('send');
    for(var x=0; x<sendNode.length; x++)
    {
        var node=sendNode[x];
        if(node.nodeName=='INPUT')
        {
            var nodeType=node.type;
            if(nodeType=='check')
            {
                if(data[node.getAttribute('name')])
                {
                    data[node.getAttribute('name')].push(node.value);
                }
                else
                {
                    var arr=[node.value];
                    data[node.getAttribute('name')] = arr;
                }

            }
            else if(nodeType=='radio')
            {
                if(node.checked)
                {
                    data[node.getAttribute('name')] = node.value;
                }
            }
            else //text, password, email
            {
                data[node.getAttribute('name')] = node.value;
            }
        }
        else if(node.nodeName=='SELECT' || node.nodeName=='TEXTAREA')
        {
            data[node.getAttribute('name')] = node.value;
        }
        else
        {
            data[node.getAttribute('data-name')] = node.innerHTML;
        }

    }
    return data;
}

      

+3
javascript dom input file-upload


source to share


No one has answered this question yet

Check out similar questions:

7649
How does JavaScript blocking work?
7494
How can I remove a specific element from an array in JavaScript?
7432
How to check if a string contains a substring in JavaScript?
5722
How can I remove a property from a JavaScript object?
4829
How do I include a JavaScript file in another JavaScript file?
3999
How do I replace all occurrences of a string?
3998
How to validate an email address in JavaScript
3796
How do you get the timestamp in JavaScript?
3714
How can I check if an array contains a value in JavaScript?
2614
How do I change the class of an element using JavaScript?



All Articles
Loading...
X
Show
Funny
Dev
Pics