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

How to set a header for a HTTP GET request, and trigger file download?(如何为 HTTP GET 请求设置标头并触发文件下载?)
本文介绍了如何为 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 请求设置标头并触发文件下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Update another component when Formik form changes(当Formik表单更改时更新另一个组件)
Formik validation isSubmitting / isValidating not getting set to true(Formik验证正在提交/isValiating未设置为True)
React Validation Max Range Using Formik(使用Formik的Reaction验证最大范围)
Validation using Yup to check string or number length(使用YUP检查字符串或数字长度的验证)
Updating initialValues prop on Formik Form does not update input value(更新Formik表单上的初始值属性不会更新输入值)
password validation with yup and formik(使用YUP和Formick进行密码验证)