iT邦幫忙

0

JS button 如何取得input 的files 檔案來讀取

  • 分享至 

  • xImage

您好:
程式碼如下:
目前想用eXECLEJS讀取XLSX檔案
但遇到一個問題
原範例他用input onchange,若連續兩次同檔名,他第二次沒有反應

想用 BUTTON 的onclick 來取得 input 內的 file,該如何來做?
var reader = new FileReader();
需要 reader.readAsArrayBuffer(file);

但 onclick 只能用 以下
var f = $("#FileUpload1").val(); //---檔名
var fileName = f.replace('C:\fakepath\', ''); // 「C:\fakepath\」 是固定名稱

取得檔名,無法取得檔案 來讀取!!!

謝謝!

<!-- <input id="FileUpload1" name="FileUpload1" type="file" onchange="parseExcelFile2(this)"   />    -->
     <input id="FileUpload1" name="FileUpload1" type="file"   />     
      <input id="Button1" type="button"  value="上傳" onclick="importXLS()" />  

增廣建文 iT邦研究生 5 級 ‧ 2023-05-13 17:16:18 檢舉
所以問題應該是卡在file input按鈕在檔名相同時無效?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

0
淺水員
iT邦大師 6 級 ‧ 2023-05-15 00:51:55
最佳解答

提供參考

<input id="FileUpload1" name="FileUpload1" type="file">
<input id="Button1" type="button"  value="上傳">
<script>
    document.querySelector('#Button1').addEventListener('click', function(evt) {
        let inputEle = document.querySelector('#FileUpload1');
        console.log(inputEle.files); // 後續用 FileReader 讀取(略)
    });
</script>
noway iT邦研究生 3 級 ‧ 2023-05-17 22:40:22 檢舉

謝謝您!

0
增廣建文
iT邦研究生 5 級 ‧ 2023-05-13 20:55:06
noway iT邦研究生 3 級 ‧ 2023-05-14 09:40:14 檢舉

您好:
應該是說
1.重覆檔名的問題
2.但想知道的是,有辦法直接用
來讀取input file 實體檔案(Blob),給後續來用嗎?

謝謝

我要發表回答

立即登入回答