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
source to share
No one has answered this question yet
Check out similar questions: