Content-Disposition附件是否已从XMLHttpRequest中阻止? [英] Is Content-Disposition attachment blocked from XMLHttpRequest?

查看:283
本文介绍了Content-Disposition附件是否已从XMLHttpRequest中阻止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我编写的C#webserver执行png文件的javascript xhr请求。
这是我使用的代码

I want to perform a javascript xhr request for a png file from a C# webserver which I wrote. Here is the code I use

    var imgUrl = "http://localhost:8085/AnImage.png?" + now;
    var request = new XMLHttpRequest();
    request.open('GET', imgUrl, false);
    request.send(); // this is in a try/catch

在服务器端我发回文件并添加Content-Disposition标头。
我获得以下回复

On the server-side I send back the file and add a Content-Disposition header. I obtain the following response

我确保Content-Disposition在Content-Type之后附加在标题中(截图来自Firebug,后者附加)按字母顺序排列。)

I made sure that Content-Disposition was attached in the headers after the Content-Type (the screenshot is from Firebug, which appends in alphabetical order).

结果是没有触发对话框,我在回复中遗漏了什么?

The results is that no dialog box is triggered, am I missing something in the response?

编辑:
我想在javascript中执行所有操作有几个原因。
首先:我不想展示图像,我想把所有东西都放在幕后。
第二:在请求图像时,我希望仅在特定请求中添加Content-Disposition。此类请求标有警告标题,其值为AttachmentRequest

edit: I want to perform everything in javascript for several reasons. First: I don't want to show the image and I want to keep everything behind the curtain. Second: when requesting the image I want the Content-Disposition to be added only on particular requests. Such requests are marked with a "Warning" header with value "AttachmentRequest"

request.setRequestHeader("Warning","AttachmentRequest");


推荐答案

我不认为当请求是通过XHR时,Content-Disposition 会触发任何文件保存对话框。使用XHR表明你将在代码中处理结果。

I don't think Content-Disposition triggers any file save dialog when the request is via XHR. The use of XHR suggests you're going to handle the result in code.

如果你想要提示用户将图像保存到文件中,我就是成功使用了这种技术:

If you want the user to be prompted to save the image to a file, I've used this technique successfully:

window.open("http://localhost:8085/AnImage.png?" + now);

它的缺点是它会短暂地闪烁一个空白的打开窗口,直到标题到达,然后是新窗口关闭并出现保存文件对话框。

It has the downside that it flashes a blank open window briefly until the header arrives, then the new window closes and the "save file" dialog box appears.

使用 iframe 可能会阻止窗口闪烁:

Using an iframe may prevent the window flashing:

var f = document.createElement('iframe');
f.style.position = "absolute";
f.style.left = "-10000px";
f.src = "http://localhost:8085/AnImage.png?" + now;
document.body.appendChild(f);

另外,我想知道效果(如果有的话) Content-Disposition 处理 img 元素:

Separately, I wonder what effect (if any) Content-Disposition has on the handling of an img element:

var img = document.createElement('img');
img.style.position = "absolute";
img.style.left = "-10000px";
img.src = "http://localhost:8085/AnImage.png?" + now;
document.body.appendChild(img);

我没试过,但浏览器可能尊重标题。您需要确保在要支持的所有浏览器上进行测试。

I haven't tried that, but the browser might respect the header. You'd need to be sure to test on all of the browsers you want to support.

这篇关于Content-Disposition附件是否已从XMLHttpRequest中阻止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆