<i id='NquBU'><tr id='NquBU'><dt id='NquBU'><q id='NquBU'><span id='NquBU'><b id='NquBU'><form id='NquBU'><ins id='NquBU'></ins><ul id='NquBU'></ul><sub id='NquBU'></sub></form><legend id='NquBU'></legend><bdo id='NquBU'><pre id='NquBU'><center id='NquBU'></center></pre></bdo></b><th id='NquBU'></th></span></q></dt></tr></i><div id='NquBU'><tfoot id='NquBU'></tfoot><dl id='NquBU'><fieldset id='NquBU'></fieldset></dl></div>

      • <bdo id='NquBU'></bdo><ul id='NquBU'></ul>
    1. <legend id='NquBU'><style id='NquBU'><dir id='NquBU'><q id='NquBU'></q></dir></style></legend>

    2. <tfoot id='NquBU'></tfoot>

      <small id='NquBU'></small><noframes id='NquBU'>

        如何为 HTTP GET 请求设置标头并触发文件下载?

        时间:2023-08-02

            <i id='y7VMM'><tr id='y7VMM'><dt id='y7VMM'><q id='y7VMM'><span id='y7VMM'><b id='y7VMM'><form id='y7VMM'><ins id='y7VMM'></ins><ul id='y7VMM'></ul><sub id='y7VMM'></sub></form><legend id='y7VMM'></legend><bdo id='y7VMM'><pre id='y7VMM'><center id='y7VMM'></center></pre></bdo></b><th id='y7VMM'></th></span></q></dt></tr></i><div id='y7VMM'><tfoot id='y7VMM'></tfoot><dl id='y7VMM'><fieldset id='y7VMM'></fieldset></dl></div>
            <legend id='y7VMM'><style id='y7VMM'><dir id='y7VMM'><q id='y7VMM'></q></dir></style></legend>

            <small id='y7VMM'></small><noframes id='y7VMM'>

            <tfoot id='y7VMM'></tfoot>

                <bdo id='y7VMM'></bdo><ul id='y7VMM'></ul>
                    <tbody id='y7VMM'></tbody>
                • 本文介绍了如何为 HTTP GET 请求设置标头并触发文件下载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  更新 20140702:

                  • 解决方案
                  • 博文中的详细答案

                  (但我将其他答案之一标记为已接受,而不是我自己的,因为它让我走到了一半,并奖励努力)

                  (but I'm marking one of the other answers as accepted instead of my own, as it got me halfway there, and to reward the effort)

                  似乎无法通过 <a href="..."> 的链接设置 HTTP 请求标头,只能使用 XMLHttpRequest.

                  It appears that setting a HTTP request header is not possible through links with <a href="...">, and can only be done using XMLHttpRequest.

                  但是,链接到的 URL 是一个应该下载的文件(浏览器不应导航到其 URL),我不确定这是否可以使用 AJAX 完成.

                  However, the URL linked to is a file that should be downloaded (browser should not navigate to its URL), and I am not sure is this can be done using AJAX.

                  另外,返回的文件是二进制文件,AJAX 不适用于此.

                  Additionally, the file being returned is a binary file, and AJAX is not intended for that.

                  如何使用添加了自定义标头的 HTTP 请求触发文件下载?

                  How would one go about triggering a file download with a HTTP request that has a custom header added to it?

                  修复断开的链接

                  推荐答案

                  试试

                  html

                  <!-- placeholder , 
                      `click` download , `.remove()` options ,
                       at js callback , following js 
                  -->
                  <a>download</a>
                  

                  js

                          $(document).ready(function () {
                              $.ajax({
                                  // `url` 
                                  url: '/echo/json/',
                                  type: "POST",
                                  dataType: 'json',
                                  // `file`, data-uri, base64
                                  data: {
                                      json: JSON.stringify({
                                          "file": "data:text/plain;base64,YWJj"
                                      })
                                  },
                                  // `custom header`
                                  headers: {
                                      "x-custom-header": 123
                                  },
                                  beforeSend: function (jqxhr) {
                                      console.log(this.headers);
                                      alert("custom headers" + JSON.stringify(this.headers));
                                  },
                                  success: function (data) {
                                      // `file download`
                                      $("a")
                                          .attr({
                                          "href": data.file,
                                          "download": "file.txt"
                                      })
                                          .html($("a").attr("download"))
                                          .get(0).click();
                                      console.log(JSON.parse(JSON.stringify(data)));
                                  },
                                  error: function (jqxhr, textStatus, errorThrown) {
                                    console.log(textStatus, errorThrown)
                                  }
                              });
                          });
                  

                  jsfiddle http://jsfiddle.net/guest271314/SJYy3/

                  这篇关于如何为 HTTP GET 请求设置标头并触发文件下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:使用 JavaScript 添加自定义 HTTP 标头 下一篇:“状态码:200 OK(来自 ServiceWorker)"在 Chrome 网络开发工具中?

                  相关文章

                • <small id='pnAzL'></small><noframes id='pnAzL'>

                    <bdo id='pnAzL'></bdo><ul id='pnAzL'></ul>
                • <tfoot id='pnAzL'></tfoot>
                • <i id='pnAzL'><tr id='pnAzL'><dt id='pnAzL'><q id='pnAzL'><span id='pnAzL'><b id='pnAzL'><form id='pnAzL'><ins id='pnAzL'></ins><ul id='pnAzL'></ul><sub id='pnAzL'></sub></form><legend id='pnAzL'></legend><bdo id='pnAzL'><pre id='pnAzL'><center id='pnAzL'></center></pre></bdo></b><th id='pnAzL'></th></span></q></dt></tr></i><div id='pnAzL'><tfoot id='pnAzL'></tfoot><dl id='pnAzL'><fieldset id='pnAzL'></fieldset></dl></div>

                      <legend id='pnAzL'><style id='pnAzL'><dir id='pnAzL'><q id='pnAzL'></q></dir></style></legend>