I want to get a file (image or video) from an
<input type='file' id='file_i'/>
// Not this <input type='submit'/>
Using an XMLHttpRequest
like this
function img() {
var fd = new FormData();
fd.append('file', document.getElementById("file_i").files[0]);
var req;
if (window.ActiveXObject) {
req=new ActiveXObject();
} else {
req=new XMLHttpRequest();
}
req.open("post", "Image", true);
req.send(fd);
}
for example.
Then in the servlet doing this:
new FileInputStream(new File(request.getParameter("file")))
How can I retrieve that file? Thanks in advance.
I fixed it. Here it is:
var fd = new FormData();
fd.append('file', document.getElementById("file_i").files[0]);
var req;
if (window.ActiveXObject) {
req=new ActiveXObject();
} else {
req=new XMLHttpRequest();
}
req.open("post", "Image", true);
req.send(fd);
@MultipartConfig
public class addImage extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
Part filePart = request.getPart("file");
InputStream fileContent = filePart.getInputStream();
}
}
<servlet>
<servlet-name>Add Image</servlet-name>
<servlet-class>servlets.addImage</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Add Image</servlet-name>
<url-pattern>/Image</url-pattern>
</servlet-mapping>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With