首页 > 解决方案 > 使用javascript处理文件打开

问题描述

我正在调用一个函数 OpenFileHotlink(),它第一次工作正常,但是当我没有选择任何要打开的文件时,它正在打开以前的文件,如果没有选择的文件,我该如何处理它,那么它不应该打开。

我试图使用超时功能清除调用堆栈。

<fieldset>
 <legend>File Name</legend>
<!----Here i am getting all files as dropdown and it changing on the basis of objectId(This is another dropdown option) and file types----->
<select id="hotlinkfilenames_Id" class="form-control">
<!-- <option value="">------Select------</option> -->
</select>
</fieldset>
<div class="col-md-12 text-center pt-10">
                                        <a  class="btn btn-success btn-xs" id="geturlOpenFileHotlink"  onclick="OpenFileHotlink()">Open</a>
</div>

function OpenFileHotlink() {
  var fileName = $("#hotlinkfilenames_Id").val();
  var id=[];
  var uri='';
  alert(fileName);

  if(fileName==null||fileName=='' ){
    alert("Please select File Name");
    //debugger;
    return false;
  }else{
    alert("");
    fileName=fileName.toString();
    id =fileName.split("/");

    var ext1=id[1].toString();
    var ext=ext1.split(".");

    var htmlOpenHotlink="";
    $.ajax({
      url:'./openFile1/'+id[0],
      async: false, 
      cache: false,
      type:'POST',
      //dataType:'json',
      success:function(response){
        uri='data:image/'+ext[1]+';base64,'+response+'';
        document.getElementById("geturlOpenFileHotlink").href = uri;
        document.getElementById("geturlOpenFileHotlink").download = id[1];
      }	
    });
  }
}

标签: javascript

解决方案


如果您只想清除输入字段,那么只需将 value 属性(或val()在 jQuery 中)设置为空字符串即可。

在 ajax 请求的成功回调中,在末尾添加

$("#hotlinkfilenames_Id").val("");

根据相关更新进行编辑

我会给你简单的(香草)js方法。(在 jquery 旁边使用 js 绝对没问题。jquery 只是一个扩展普通 js 的库)。

document.getElementById('hotlinkfilenames_Id').innerHTML = '';

将此添加到您的成功方法中,它将清除选择下拉列表。希望能帮助到你。


推荐阅读