如何在不提交的情况下使用文件上传控件获取客户端选定的文件名?

How can I obtain selected filenames clientside using a fileupload control without submitting?

到目前为止我找不到现有的答案,但这是我的目标:

我想使用 asp.net 文件上传控件(或您可能建议的其他控件)来 select 一系列文件。在不提交表单的情况下,我希望 selected 文件显示在某种串联列表中。

使用 Fileupload 控件是否可行,还是我必须使用其他控件?我已经看到很多涉及提交然后获取文件名的答案,但我不想为此去服务器。只是想在 selected 时获取文件名,所有客户端。

感谢您的宝贵时间!

使用 files 属性。请看下面的代码:

function onInputChange(e) {
    var res = "";
    for (var i = 0; i < $("#customInput").get(0).files.length; i++) {
      res += $("#customInput").get(0).files[i].name + "<br />";
    }
  
    $('#result').html(res);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="customInput" type="file" accept="image/*" multiple onchange="onInputChange(event)" />
<br /><br />
<div style="color:blue" id='result'></div>

试试这个 -

Javascript

 function showFile() {
  var file = document.getElementById("<%=FileUpload1.ClientID%>");      
  var path = file.value;
  alert(path); 
}

文件上传控制

<asp:FileUpload ID="FileUpload1" runat="server" onchange="showFile()"  />