如何在不强制“另存为"的情况下设置响应文件名.对话 [英] How to set response filename without forcing "save as" dialog

查看:147
本文介绍了如何在不强制“另存为"的情况下设置响应文件名.对话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在某些响应中返回了一个流,并设置了适当的content-type标头.我正在寻找的行为是这样的:

I am returning a stream in some response setting the appropriate content-type header. The behavior I'm looking for is this:

  • 如果浏览器能够呈现给定内容类型的内容,则应将其显示在浏览器窗口中.

  • If the browser is able to render content of the given content type then it should display it in the browser window.

如果浏览器不知道如何呈现内容,则应显示另存为"对话框,其中文件名应为响应中提供的文件名.

If the browser doesn't know how to render the content, then it should display the "save as" dialog where the filename should be the one provided in the response.

问题是,如果我将Content-Disposition标头设置为:

The problem is that if I set the Content-Disposition header with:

附件; filename =" myfile.txt"

"attachment; filename="myfile.txt""

浏览器将始终显示另存为"对话框.

the browser will always display the "save as" dialog.

如果我未设置Content-Disposition,则另存为"对话框中使用的文件名将是URL上无法使用的文件名.

If I don't set Content-Disposition, the filename used in the "save as" dialog is the one in the URL that doesn't work in my case.

我也尝试将Content-Disposition设置为inline,但是结果是相同的.

I also tried setting Content-Disposition to inline but the outcome is the same.

推荐答案

正确的方法可能是:

Content-Disposition: inline; filename="myfile.txt"

这篇关于如何在不强制“另存为"的情况下设置响应文件名.对话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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