下载文件后操作表单不起作用

时间:2019-06-07 14:49:20

标签: javascript java html

我用javascript,java和html5编写了一些代码。概括起来,我有一个带有html中给定输入值的表单,并将这些值传递给javascript函数,该函数生成bat文件,并在要求输入密码后使其可下载。之后,应该执行form操作。

我显示的密码检查不是我当前使用的密码。我的问题是:每当我运行该代码时,如果我单击按钮,它都会要求我输入密码,如果匹配,就可以下载该动态生成的脚本。但是,如果通过脚本,它将不执行“ MoveNotification”(我将控制台输出放在element.click()之后,但未达到该点)。我意识到问题出在element.click(),如果我注释掉那行,一切正常,但没有下载提示。如果输入了错误的密码,或者只是在提示框中按“取消”,它将基本上按照与我所需要的相反的方式运行它。

我尝试手动设置一个隐藏的输入并使用javascript填充它,但这根本不起作用。


function downloadScript(form) {
  var psw = prompt("Inserisci password mail:", "password");
  var correct = document.getElementById('vpassword').value;
  if (correct == psw) {
    var scriptText = 'bat script to move a file and delete itself using data from hidden form input';
    var element = document.createElement('a');
    element.setAttribute('href', 'data:application/octet-stream, ' + encodeURIComponent(scriptText));
    element.setAttribute('download', "filename.bat");
    element.style.display = 'none';
    form.appendChild(element);
    element.click();
//if i put a console output here, it's not executed
    form.removeChild(element);
  }
//I don't even need a return true or an else statement since the script stops after element.click()
  window.alert('Wrong Password');
}

<form id=\"formMail\" name=\"formMail\" method=\"post\" action=\"MoveNotification\" onSubmit=\"downloadScript(this)\">
<input type="hidden" name="vpassword" id="vpassword" value="password">
<img src=\"img/ico/mail.gif\" width=\"30\" height=\"30\"/>
<script>Here I wrote the above script</script>
</form>
</button>

MoveNotification.java是我想在脚本之后(或之前,无论如何)运行的东西。我希望脚本在element.click()和要执行的表单中指定的操作之后恢复。谢谢大家!

0 个答案:

没有答案