Cómo crear y enviar un formulario utilizando Javascript
-
22-09-2019 - |
Pregunta
Estoy escribiendo una extensión para Firefox que es para ser usado en casa. Básicamente, tengo que enviar un archivo PDF y algunos valores de texto a un servicio web. En este momento tengo una página HTML que hace esto. Necesito la extensión para automatizar la recopilación y presentación de los datos para el servicio web.
Aquí está el código HTML que funciona.
<body>
<form name="frm_upload_file" enctype="multipart/form-data" method="post" action="my web service address">
<table cellpadding="2" cellspacing="0" border="0">
<tr>
<td>ClientID</td>
<td><input type="text" name="client_id" /></td>
</tr>
<tr>
<td>HTML</td>
<td><input type="text" name="html" /></td>
</tr>
<tr>
<td align="right" class="inputtxt"> File: </td>
<td class="inputtxt">
<input name="pdf" type="file" />
</td>
</tr>
<tr>
<td align="left" colspan="2" class="inputtxt">
<p><br /><input type="submit" name="submit" value="Submit" /></p>
</td>
</tr>
</table>
Aquí está el código JavaScript que no lo hace
postToURL: function(html, file) {
var form = content.document.createElement("form");
form.setAttribute("enctype", "multipart/form-data");
form.setAttribute("method", "post");
form.setAttribute("action", "address to my web service");
var clientIDField = document.createElement("input");
clientIDField.setAttribute("type", "text");
clientIDField.setAttribute("name", "client_id");
clientIDField.setAttribute("value", "123456");
form.appendChild(clientIDField);
var htmlField = document.createElement("input");
htmlField.setAttribute("type", "text");
htmlField.setAttribute("name", "html");
htmlField.setAttribute("value", html);
form.appendChild(htmlField);
var fileField = document.createElement("input");
fileField.setAttribute("type", "file");
fileField.setAttribute("name", "pdf");
fileField.setAttribute("value", file);
form.appendChild(fileField);
content.document.body.appendChild(form);
form.submit();
Al presentar los datos con los js, tengo la siguiente excepción del servidor. excepción
javax.servlet.ServletException: com.sun.jersey.api.container.ContainerException: javax.mail.MessagingException: Falta inicio límite
¿Alguna idea?
Solución
No se puede establecer el valor de una entrada de archivo -. De lo contrario podría robar archivos de máquinas de la gente
Otros consejos
No creo que se puede modificar el valor de un campo <input type="file">
por razones de seguridad. Es probable que tengas que encontrar otra manera de automatizar esto.