Reputation: 807
This was jsp file uploading form
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>File Uploading form</title>
</head>
<body>
<form action="./read.do" method="post">
Select the file to read: <input type="file" id="myFile" size="50">
<br />
path is: <input type="text" name="path" />
<button type="button" onclick="myFunction()">Copy path</button>
<input type="submit" value="Submit">
<p id="demo"></p>
<script>
function myFunction() {
var x = document.getElementById("myFile").value;
document.getElementsByTagName("INPUT")[3].setAttribute("value",x);
}
</script>
</form>
</body>
</html>
Here i was not uploading the file to my server i was just taking the path of the file and reading the file using following code in servlet
String path = req.getParameter("path");
FileInputStream fis = null;
File excel = new File(path);
fis = new FileInputStream(excel);
this works fine when i run on same machine where server is present .But when i try to access from other machine the code is searching the path in my machine but not on client machin how to over come this. I have seen some other ways like using multipart but there the file is loading to server but for my requirement i should not upload the file directly to my server i have to read the complete file on client machine it self by taking the file path . Please suggest me some ideas to attain this
Upvotes: 1
Views: 4747
Reputation: 580
You can try JavaScript File Reader API for reading the file contents in the client side using JS. file API
Ex:
var file = document.getElementById("fileForUpload").files[0];
if (file) {
var reader = new FileReader();
reader.readAsText(file, "UTF-8");
reader.onload = function (evt) {
document.getElementById("fileContents").innerHTML = evt.target.result;
}
reader.onerror = function (evt) {
document.getElementById("fileContents").innerHTML = "error reading file";
}
}
or you can also go for HTML5 's file Reader HTML5 FileReader
Upvotes: 2
Reputation: 26
File upload is a multi-part request. request.getParameter() returns the file path on the client machine. Instead of that, you need to use either request.getPart() method or libraries like apache-fileupload to retrieve the inputstream of the remote file.
Upvotes: 1