I want to upload an excel file to a html/jsp page and send the file path to my Java controller. Here is my code snippet:-
The jsp:
<script>
function getPath()
{
document.getElementById("actualPath").value=document.getElementById("excelFile").value;
console.log(document.getElementById("actualPath").value);
return false;
}
</script>
</head>
<body>
<form action="<%=request.getContextPath()%>/Controller?action=uploadExcel" onsubmit="return getPath()" method="POST">
<input type="file" name="excelFile" id="excelFile">
<input type="hidden" name="actualPath" id="actualPath" value="">
<input type="submit">
</form>
The Java Code:-
private void getExcel(HttpServletRequest request,
HttpServletResponse response) throws BiffException, IOException, JSONException, ServletException {
System.out.println(request.getParameter("excelFile"));
String FilePath = request.getParameter("actualPath");
Workbook workbook = Workbook.getWorkbook(new File(FilePath));
Sheet sh = workbook.getSheet("Sheet1");
int totalNoOfRows = sh.getRows();
Map<String,String> map=new LinkedHashMap<String, String>();
int totalNoOfCols = sh.getColumns();
I get the actualPath value to be C:\fakepath\Book1.xls which is obvious since its not allowed to get the file path. Is there any way around by which I could solve this. Or send the file to Java directly?? Thanks in advance