How to set a header for a HTTP GET request, and trigger file download?

前端 未结 5 1266
不思量自难忘°
不思量自难忘° 2020-11-27 03:02

Update 20140702:

  • The solution
  • Detailed answer as a blog post

(but I\'m marking one of the other answers as accepted in

5条回答
  •  情话喂你
    2020-11-27 03:23

    Pure jQuery.

    $.ajax({
      type: "GET",
      url: "https://example.com/file",
      headers: {
        'Authorization': 'Bearer eyJraWQiFUDA.......TZxX1MGDGyg'
      },
      xhrFields: {
        responseType: 'blob'
      },
      success: function (blob) {
        var windowUrl = window.URL || window.webkitURL;
        var url = windowUrl.createObjectURL(blob);
        var anchor = document.createElement('a');
        anchor.href = url;
        anchor.download = 'filename.zip';
        anchor.click();
        anchor.parentNode.removeChild(anchor);
        windowUrl.revokeObjectURL(url);
      },
      error: function (error) {
        console.log(error);
      }
    });
    

提交回复
热议问题