Javascript upload image using ajax without user interaction

193 Views Asked by At

I am trying to send an image to an image recognition API called Cloudsight. I have gotten it working using a URL, and am now trying to get it to send a local image. The thing is, it can't require any user interaction, as it all has to happen automatically. The answers I've seen use FormData, but as it can't require user interaction I don't think I can use it. (unless there's a way to use it to upload something automatically.) The code currently is:

var token; //Variable for use later

//First AJAX request.
$.ajax({
  method: "POST",
  url: "https://api.cloudsightapi.com/image_requests",
  beforeSend: function(xhr) { // Authorizes the request.
    xhr.setRequestHeader("Authorization",
      "CloudSight [key]");
  },
  data: { // The data to send.
    "image_request[image]": //the image needs to go here,
    "image_request[locale]": "en-US"
  },
  success: function(msg) { // What should happen if succesful.
    console.log("It worked! :D Good POST request.");
    console.log(msg);

    token = msg.token; // Assigns the token the POST request returns to the token variable.

    get(); // Calls the function containing the GET request.
  },
  error: function(msg) { // What should happen if not succesful.
    console.log("Sorry...");
    console.log(msg);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

All I need is something to send the local file; how do I upload the image without user interaction?

1

There are 1 best solutions below

0
On

Technically you can do

netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserWrite'); in a netscape-compatible browser (Firefox, Mozilla, Netscape), and it will ask the user* whether or not to allow filesystem access, but this is not portable.

*once per browser processQ