user2110793
user2110793

Reputation: 21

Custom file picker without using <input type="file"/>

I am working on a java-based (webwork framework) web-app where the file to be uploaded needs to be compressed first. Since there's no way to set the value of "input type='file'" element via javascript, I decided to take the route of an embedded applet. Basically this applet compresses the chosen file, then uploads the compressed file to the server via scp.

It worked well, but I have issues regarding the rendering of the web page itself. I am thinking instead of implementing the file picker within the applet, if there's an existing file picker that I can use instead. Of course without putting any "input type='file'".

Links to these existing custom web file pickers will be very much appreciated.

Upvotes: 2

Views: 5434

Answers (2)

TheRealChx101
TheRealChx101

Reputation: 1544

This always works.

<div id="input_container" style="width: 0px; height: 0px; overflow: hidden"><input type="file" id="inputfile" /></div>
<div class="button" onclick="upload();">Upload file</div>

And your script

function upload(){
 document.getElementById('inputfile').click();
}

Your CSS

.button {
   /*button style here*/
}

Upvotes: 4

Taha Jahangir
Taha Jahangir

Reputation: 4902

Due to security restrictions the only way to select a file with HTML is adding an <input type=file> to document. Then user should selects a file with real clicks.

Note that javascript is able (in modern browsers) to read the content of file, so it should not be able to select arbitrary file and read it.

Upvotes: 2

Related Questions