Javascript使用ajax上传图像,无需用户交互

Javascript upload image using ajax without user interaction

本文关键字:用户 交互 图像 使用 ajax Javascript      更新时间:2023-09-26

我正在尝试将图像发送到名为Cloudsight的图像识别API。我已经使用URL让它工作,现在正试图让它发送一个本地图像。问题是,它不需要任何用户交互,因为这一切都必须自动发生。我看到的答案使用FormData,但由于它不需要用户交互,我认为我不能使用它。(除非有一种方法可以使用它自动上传东西。)目前的代码是:

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>

我所需要的只是发送本地文件;如何在没有用户交互的情况下上传图像?

从技术上讲,您可以进行

netscape.security.PrivilegeManager.enablePrivilege('UniversalBrowserWrite');在netscape兼容的浏览器(Firefox、Mozilla、netscape)中,它会询问用户*是否允许文件系统访问,但这是不可移植的。

*每个浏览器进程一次Q