I'd like to know how can I upload an input file to my database (MySQL) with just JavaScript (using JSP). I've been searching for days & I haven't got luck.
All I do is: grab the inputs' values with 'getElementById' from the HTML code, then I send them to a JSP which it catches them with 'request.getParameter' & put them into their SETS so they can be uploaded into the database. All I want is upload a file (pic, xls, doc or PDF).
(with .value, it shows me: "C:/fakepath/file.jpg" [Browsers' Security rules]. How can I get the file's value & send it to the JSP where I put it into its SETS & then upload it?).
Well, my HTML looks like:
<table>
<tr>
<td>
Name: <input type="text" name="yourName" id="yourName" />
</td>
</tr>
<tr>
<td>
Photo: <input type="file" name="yourFile" id="yourFile" />
</td>
</tr>
<tr>
<td>
<button type="button" onClick="Save()">Save</button>
</td>
</tr>
</table>
My JavaScript looks like this:
function nuevoAjax() {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
function Save(){
ajax = nuevoAjax();
var name = document.getElementById("yourName").value;
var photo = document.getElementById("yourFile").value; //.value(?)
ajax.open("POST", "saveData.jsp?name="+name+"&photo="+photo, true);
ajax.onreadystatechange = function () {
if (ajax.readyState == 4) {
alert(ajax.responseText);
}
}
ajax.send();
}
& my saveDate.jsp looks like:
<%
String name = request.getParameter("name");
XXXXX photo = request.getParameter("photo");
objUser.setName(name);
objUser.setPhoto(photo);
objUser.saveUser(); // This method has: INSERT INTO & returns Msg (Uploaded / Error)
out.print(objUser.getMsg());
%>
I've been breaking my back trying' to find something that helps me with this for days. Thanks in advance!