-4

So there is this code

<div>DROP!<button onclick="document.querySelector('input').click()">Or click</button>    </div>
<input style="visibility: collapse; width: 0px;" type="file" onchange="upload(this.files[0])">

<script>


window.ondragover = function(e) {e.preventDefault()}
window.ondrop = function(e) {e.preventDefault(); upload(e.dataTransfer.files[0]); }
function upload(file) {

    if (!file || !file.type.match(/image.*/)) return;

And what it does is take a file from the user, checks if it's an image and then does something. How can I make it so that instead of letting the user select the image I select the path of the image and then it does what it's supposed to do. So like declare the file I think.

user2526311
  • 1,004
  • 2
  • 10
  • 19

1 Answers1

1

A webpage does not have permission to select a file from a visitor's computer. That would be a serious security risk (e.g. Popular accounting software stores its data in file X by default, webpage selects that file, reads it, and sends the data to the site owner).

All selections of local files on a webpage must be performed by the user.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • So I can't give it the path of a file. Any other way I can do this without the user having to input the location? – user2526311 Jun 30 '13 at 00:02
  • 1
    Correct. No, that would be a security hole. – Quentin Jun 30 '13 at 00:03
  • Another quick question, is there anyway I can integrate this with a Java program? And when a file is made with Java use this to upload it? – user2526311 Jun 30 '13 at 00:04
  • Assuming that by "Java program" you mean "A Java application" (as opposed to a Java applet or a Java servlet), then that program can upload the file and not involve a webpage or browser or JavaScript at all. Just find a suitable HTTP client library for Java. – Quentin Jun 30 '13 at 00:06
  • http://stackoverflow.com/questions/1322335/what-is-the-best-java-library-to-use-for-http-post-get-etc – Quentin Jun 30 '13 at 00:07