I'm doing file upload using XMLHttpRequest()
in my jsp and when I do request.getContentType() in my controller I'm getting:
multipart/form-data; boundary=---------------------------4664151417711.
Further I'm not getting how to get the file and get the contents of it in my controller. Please anyone help.
Update -- I'm doing this in my jsp.
function fileUpload() {
var url= document.getElementById("urlId").value;
var file= document.getElementById("xslId").files[0];
var formdata = new FormData();
formdata.append("url", url);
formdata.append("file", file);
var xhr = new XMLHttpRequest();
xhr.open("POST","http://localhost:8080/XlsUpload/openSource.htm", true);
xhr.send(formdata);
xhr.onload = function(e) {
};
}
and in my controller--
public void openSource(@ModelAttribute("domTool") DomTool domTool,HttpServletRequest request,HttpServletResponse response){
String type=request.getContentType();
Further I'm struck how to get the contents of the uploaded file and the value of text field i.e.,URL in my controller. The type i'm getting as multipart/form-data
There is an Apache commons solution called commons-fileupload
for parsing multipart content. You can find it here.
The most simple example copied from their tutorial looks like this:
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = upload.parseRequest(request);
// iterate over items (i.e. list of FileItem) and access
// the content with getInputStream()
}