空的Excel文件已用JavaScript上传

时间:2018-08-20 03:29:19

标签: javascript ajax file-upload postfile

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

<!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文件?

0 个答案:

没有答案