How to get file name for <input type = "file" in jsp

I want to read the file path from html input type="file" (the entry selected in the file dialog by the user)

<script>   
    function OpenFileDialog(form) {    
        var a = document.getElementById("inputfile").click();
        SampleForm.filePath.value = //set the path here
        document.SampleForm.submit();   
    } 
</script>

<form name="SampleForm" action="TestServlet" method="get">
    <input type="file" style="display:none;" id="inputfile"/> 
    <a href="javascript:OpenFileDialog(this.form);">Open here</a>
    <input type="hidden" name="filePath" value=""/> 
</form>

I want the path to the selected file to be read in my Servlet class. How to get the path to the file? Can i read it with var a? Or is there a way to directly access the file path from input type="file"from my servlet?

+3
source share
2 answers

-, : . , , c:/passwords.txt, , ? java.io.File? ? , . - .

-, : Javascript input type="file" - . , -, c:/passwords.txt onload. , -! ?

, . HTML-, POST multipart/form-data <form>.

<form action="upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit">
</form>

, . API Servlet 2.5 mulipart/form-data, . - Apache Commons FileUpload . , ​​. Servlet 3.0, API , HttpServletRequest#getParts() . .

+9
0

Source: https://habr.com/ru/post/1740151/


All Articles