Javascript 如何通过ajax使用Springboot@RestController下载文件

Javascript 如何通过ajax使用Springboot@RestController下载文件,javascript,xmlhttprequest,Javascript,Xmlhttprequest,我有一个Springboot rest控制器来下载文件。我正在尝试使用浏览器访问端点,并且能够在xhr.response中看到响应。但是,我想强制下载我无法实现的文件 代码如下: 基于Rest的端点(Springboot): 你可以这样试试 xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.response != "0") { console.log(xhr)

我有一个Springboot rest控制器来下载文件。我正在尝试使用浏览器访问端点,并且能够在xhr.response中看到响应。但是,我想强制下载我无法实现的文件

代码如下: 基于Rest的端点(Springboot):


你可以这样试试

xhr.onreadystatechange = function () {
     if (xhr.readyState == 4 && xhr.response != "0") {
         console.log(xhr)
        var windowUrl = window.URL || window.webkitURL;
        var url = windowUrl.createObjectURL(xhr);
        anchor.prop('href', url);
        anchor.prop('download', "filename");
        anchor.get(0).click();
        windowUrl.revokeObjectURL(url);
         console.log("Downloaded file");
     } else {
        console.log("error downloading document")
     }
  }
xhr.send();

你可以这样试试

xhr.onreadystatechange = function () {
     if (xhr.readyState == 4 && xhr.response != "0") {
         console.log(xhr)
        var windowUrl = window.URL || window.webkitURL;
        var url = windowUrl.createObjectURL(xhr);
        anchor.prop('href', url);
        anchor.prop('download', "filename");
        anchor.get(0).click();
        windowUrl.revokeObjectURL(url);
         console.log("Downloaded file");
     } else {
        console.log("error downloading document")
     }
  }
xhr.send();

我发现问题出在前端,与后端无关,因为Springboot正在返回正确的响应

在上面Dinesh提供的答案的基础上,我修改了我的答案如下

download(id, name){

    var xhr = new XMLHttpRequest();
    xhr.open("GET", http.getHRTargetURL() + "download/" + id, true);
    xhr.setRequestHeader("X-Auth-Token", http.getToken());
    xhr.setRequestHeader("Content-Type", "application/octet-stream");
    xhr.responseType       = "blob";

    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4) {
        var a = document.createElement("a");
        document.body.appendChild(a);
        a.style    = "display: none";
        var url    = window.URL.createObjectURL(new Blob([xhr.response], {type: "octet/stream"}));
        a.href     = url;
        a.download = name;
        a.click();
        window.URL.revokeObjectURL(url);
      } else {
        console.log("error downloading document")
      }
    }
    xhr.send();

  }

我发现问题出在前端,与后端无关,因为Springboot正在返回正确的响应

在上面Dinesh提供的答案的基础上,我修改了我的答案如下

download(id, name){

    var xhr = new XMLHttpRequest();
    xhr.open("GET", http.getHRTargetURL() + "download/" + id, true);
    xhr.setRequestHeader("X-Auth-Token", http.getToken());
    xhr.setRequestHeader("Content-Type", "application/octet-stream");
    xhr.responseType       = "blob";

    xhr.onreadystatechange = function () {
      if (xhr.readyState == 4) {
        var a = document.createElement("a");
        document.body.appendChild(a);
        a.style    = "display: none";
        var url    = window.URL.createObjectURL(new Blob([xhr.response], {type: "octet/stream"}));
        a.href     = url;
        a.download = name;
        a.click();
        window.URL.revokeObjectURL(url);
      } else {
        console.log("error downloading document")
      }
    }
    xhr.send();

  }

有一个打字错误。您应该将windowUrl更改为window.url有一个输入错误。您应该将windowUrl更改为window.URL