简体   繁体   English

我如何允许用户输入#,它会使用正确的#找到我的本地zip文件之一并继续下载?

[英]How can I allow the user input a # , which finds one of my local zip files with correct # and proceeds to download it?

I am currently creating a simple HTML page, that allows a user to input a "#", then with a click of a button, it will download the respective "#.zip". 我当前正在创建一个简单的HTML页面,该页面允许用户输入“#”,然后单击一个按钮,它将下载相应的“#.zip”。

<!DOCTYPE html>    
<h2>Enter FileName # to start download...</h2>    
<input type="text" id="refID" title="Enter FileName # to start download">    
<button type= "file" onclick="Download()">Download</button>    
<script>
    function Download() {

      var x = document.createElement('a');
      var y = "ModifiedIndex/"+document.getElementById("refID").value+".zip";
      x.href = y;
    }
</script>

I expect a download to occur, but either the page will go blank, or it does nothing and stays on the page. 我希望进行下载,但是该页面将变为空白,或者什么都不做,而是停留在该页面上。

The thing that might be causing your page to go blank is <button type="file"> . 可能导致页面空白的是<button type="file"> type="file" doesn't exist, so it falls back to the default of submitting the form. type="file"不存在,因此回退到提交表单的默认值。 (There isn't one visible, but if the real code has one…) Use type="button" . (没有一个可见的,但是如果实际代码中有一个……)使用type="button"

The thing that's causing the download not to happen is that you're creating a link, setting where the link goes to, and throwing the link away. 导致下载无法进行的原因是,您正在创建链接,设置链接的位置以及将链接丢弃。 Navigate to the URL instead: 导航到URL:

function Download() {
  location.href = "ModifiedIndex/"+document.getElementById("refID").value+".zip";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM