简体   繁体   English

上传 excel 文件时显示进度条

[英]Display Progress Bar when upload excel file

i'm trying to make web that include uploading excel and store the data in database.我正在尝试制作 web ,其中包括上传 excel 并将数据存储在数据库中。 when uploading the excel data, i would like to display progress bar because usually it will take large file.上传 excel 数据时,我想显示进度条,因为通常它会占用大文件。 As of now i read the excel file and parsing it with XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName], {defval: ""});截至目前,我阅读了 excel 文件并使用XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName], {defval: ""});

I have already tried solution from internet, but it all just simple activity like upload to put it in another folder, i would like to make progress bar where the value of progress bar indicate how many row have been parsed.我已经尝试过来自互联网的解决方案,但这只是简单的活动,比如上传将它放在另一个文件夹中,我想制作进度条,其中进度条的值指示已解析了多少行。 but with XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName], {defval: ""});但使用XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName], {defval: ""}); it parsing all of the row in once so the progress bar will go from 0 to 100 at once.它一次解析所有行,因此进度条将 go 一次从 0 到 100。

can you guys please help me, how to make progress bar with this situation or do you guys know how to parsing excel row by row not at once?你们能帮我吗,如何在这种情况下制作进度条,或者你们知道如何不一次逐行解析 excel 吗?

btw, i'm building it with php and javascript顺便说一句,我正在用 php 和 javascript 构建它

 $("#fileUploader").change(function(evt) { document.getElementById("progressBar").value = 0; document.getElementById("status").innerHTML = "Uploading..."; var selectedFile = evt.target.files[0]; var reader = new FileReader(); reader.onload = function(event) { var data = event.target.result; var workbook = XLSX.read(data, { type: 'binary' }); workbook.SheetNames.forEach(function(sheetName) { var XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName], { defval: "" }); var json_object = JSON.stringify(XL_row_object); $.ajax({ type: "POST", url: "add.php", data: { json: JSON.stringify(XL_row_object) } }); var percent = (event.loaded / event.total) * 100; document.getElementById("progressBar").value = Math.round(percent); }) }; reader.onerror = function(event) { console.error("File could not be read. Code " + event.target.error;code); }. reader;readAsBinaryString(selectedFile); });
 <label for="myfile">Select a file:</label> <form id="upload_form" enctype="multipart/form-data" method="post"> <input type="file" name="fileUploader" id="fileUploader"><br> <progress id="progressBar" value="0" max="100" style="width:300px;"></progress> <h3 id="status"></h3> </form>

PS: i don't know how to put the xlxs script so you will get an error if you run in here PS:我不知道如何放置 xlxs 脚本,所以如果你在这里运行会出错

$("#fileUploader").change(async evt => {
  const [progressBar, status] = document.querySelectorAll('#progressBar, #status')
  progressBar.value = 0
  status.innerText = 'Uploading'

  const [selectedFile] = evt.target.files;
  const data = await selectedFile.arrayBuffer()
  const workbook = XLSX.read(new Uint8Array(data), {
    type: 'array'
  })

  workbook.SheetNames.forEach(sheetName => {
    const XL_row_object = XLSX.utils.sheet_to_row_object_array(workbook.Sheets[sheetName], {
      defval: ""
    });
    const json = JSON.stringify(XL_row_object)
    const xhr = new XMLHttpRequest()
    xhr.upload.onprogress = evt => {
      const percent = (evt.loaded / evt.total) * 100;
      progressBar.value = Math.round(percent)
    }
    xhr.send(json)
  })
})

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

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