javascript - 如何一键上传文件

标签 javascript jquery servlets

我有 HTML 页面,它提供了将文件上传到服务器的选项,服务器端有一个 servlet 操作类,它处理请求并写入文件。 工作原理

  1. 点击浏览文件并选择文件
  2. 点击提交按钮。

但我想在任何按钮上单击一次即可上传,请查看我的代码并建议如何使用 javascript/jQuery fn 来完成此操作。

<html> <head>  
  <script type = "text/javascript">
      function test() {
         var uploadfile = document.getElementByID("upload_id");
        uploadfile.click();  // here i can browse the file without clicking on file brows button
} </script>  </head> <body>

  <input type="button" id="just_one_click" value ="click me" onclick="test();" />

 <form action="upload.do" method="post" enctype="multipart/form-data">

   <input type="file" id="upload_id" name = "fileupload" />
   <input type = "submit" value="upload" />

 </form>  </body> </html> 

最佳答案

您可以为您的表单分配 ID(假设为“myform”)并使用 document.getElementById('myform').submit(); 而不是 uploadfile.click();

关于javascript - 如何一键上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6772821/

相关文章:

javascript - angularjs 一个元素上的两个指令

javascript - 在 HTML 区域标记中使用图像

java - Paypal 业务标准 : How to handle action after the payment?

java - 服务程序 : weird cookie string

java - servlet如何处理一次请求中上传多个文件

php - JavaScript 对象共享局部变量

javascript - 错误 TS2339 : Property 'access_token' does not exist angular 2

javascript - ExtJS 4 Ext.grid.Panel 选择颜色

PHP Ajax 查询 - 将多个文件上传为 blob

javascript - Textarea charCount - 防止用户粘贴