gpt4 book ai didi

javascript - 如何一键上传文件

转载 作者:行者123 更新时间:2023-11-29 22:31:22 26 4
gpt4 key购买 nike

我有 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com