1

我正在尝试通过单击操作上传和下载 excel 文件。我在一次操作中调用了两个 API。下载操作正常。但是在文件上传期间,正在上传一个带有确切文件名的空 excel 文件。这是 JAVA 中的服务器端代码 Excel Duplicates Remover。服务器端工作正常。这是客户端代码。

<!DOCTYPE html>
<html>
   <body>
      Select a file: <input type="file" id="file"  accept=".csv, .xls, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel" multiple size="50"/><br>
      Keyword:<input type="text" id="columnHeading"/><br>
      <button type="button" onclick="downloadDoc()">Request data</button>
      <script>
         function downloadDoc(){
         var fileTab = document.getElementById("file");
         var columnHeading = document.getElementById("columnHeading");
         if('files' in fileTab) {
         if(fileTab.files.length == 1) {
         var url = 'http://localhost:8080/excel-duplicate-remover/rest/fileService/';
         var uploadUrl = url + "upload";
         var file=fileTab.files[0];
         var formData = new FormData();
         formData.append("columnHeading",columnHeading);
         formData.append("file",file);
         var xhr = new XMLHttpRequest();
         xhr.onreadystatechange = function() {
             if (this.readyState == 4 && this.status == 200) {
         var response = JSON.parse(this.responseText);
         var path = response.path;
         var downloadUrl = url + 'downloadFile?fileName=' + path;
         downloadFile(downloadUrl);
             }
           };
         xhr.open("POST", uploadUrl);
         xhr.send(formData);
         }else{
         console.log("file not present");
         }
         }
         }

         function downloadFile(filename) {
         console.log("filename"+filename);
           var link = document.createElement('a');
             // Add the element to the DOM
             link.setAttribute("type", "hidden"); // make it hidden if needed
             link.href = filename;
             link.download;
             document.body.appendChild(link);
             link.click();
             link.remove();
         }
      </script>
   </body>
</html>

为什么文件被上传为空的 excel 文件?

4

0 回答 0