在IE 9和Firefox 13中忽略了内容处置 [英] Content-disposition being ignored in IE 9 and Firefox 13

查看:215
本文介绍了在IE 9和Firefox 13中忽略了内容处置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图动态创建一个内联PDF,当用户选择保存它时,提示用我的自定义文件名。根据文档 saveasname 属性应该做我想要的。


(format =PDF)SaveAs对话框中显示的文件名用户保存写入浏览器的PDF文件。


但是,IE 9和Firefox 13.0.1在SaveAs对话框中出现的文件名与我的CF模板相同,但是具有PDF扩展名。 (换句话说,我的代码在 makepdf.cfm 中,SaveAs提示我保存 makepdf.pdf 。但在Chrome中,它工作得很好。 (所有在Windows 7上)



这是我的代码来创建PDF:

 code>< cfdocument format =pdfbookmark =truesaveasname =MyReport.pdf> 

如果我明确声明内容处置和内容类型,例如

 < cfheader name =Content-Dispositionvalue =inline; filename = MyReport.pdf> 
< cfcontent type =application / x-pdf>
< cfdocument format =pdfbookmark =truesaveasname =MyReport.pdf>




  • Chrome告诉我Content-Disposition / li>
  • Firefox告诉我PDF文件已损坏

  • IE忽略它(仍然不会显示正确的文件名)



如果我只是依靠头

  cfheader name =Content-Dispositionvalue =inline; filename = MyReport.pdf> 
< cfcontent type =application / x-pdf>
< cfdocument format =pdfbookmark =true>

我得到与第一段代码相同的行为。



我知道如何让浏览器提示下载,而不是内联显示,一切都按预期工作,但这不是想要的行为。



我需要在文件名中使用时间和日期,最终用户不够精明,不能覆盖他们的文件(如果他们选择保存它们)。



我缺少的东西会得到IE和Firefox做他们应该做什么?还有什么其他浏览器会这样做?移动Safari?

解决方案

问题似乎是filename = xxx真的用于附件处置,不是所有的浏览器PDF插件都认为它是一种用于指定内联另存为的机制,正如您所发现的。



一种不同的方法,您的首选文件名将是使用Web服务器重写规则操纵URL。作为一个简单的示例,您可以使用脚本生成PDF并在内部提供: pdf.cfm

 < cfheader name =Content-Dispositionvalue =inline> 
< cfdocument format =PDFmimetype =application / pdf>测试< / cfdocument>

然后创建一个与URL匹配的重写规则,格式为 / pdf / myfilename ,并将它们传递到 pdf.cfm 。在IIS7上,这可能是:

 < rule name =Inline PDF SaveAsstopProcessing =true> 
< match url =^ / pdf / [\w - ] + $ignoreCase =true/>
< action type =Rewriteurl =/ pdf.cfmappendQueryString =false/>
< / rule>

这将匹配仅包含字母数字,下划线和连字符的文件名。



当您访问 / pdf / myreport 时,PDF将以内联方式显示该插件,当您保存它,默认文件名将 myreport.pdf



如果你使用的框架支持搜索



更新:实际上,您不需要使用网址重写:只需附加正斜杠,然后将所需的文件名替换为CF脚本URL,例如



/pdf.cfm/myreport



插件将使用最后一个斜杠后面的任何内容作为另存为...名称。


I am trying to dynamically create an inline PDF that, when the user chooses to save it, prompts with my custom filename. According to the documentation, the saveasname attribute should do what I want.

(format="PDF" only) The filename that appears in the SaveAs dialog when a user saves a PDF file written to the browser.

However, what is happening in both IE 9 and in Firefox 13.0.1 is that the filename that appears in the SaveAs dialog is the same as my CF template, but with a PDF extension. (In other words, my code is in makepdf.cfm and the SaveAs prompts me to save makepdf.pdf.) In Chrome, however, it works perfectly. (All on Windows 7.)

Here is my code to create the PDF:

<cfdocument format="pdf" bookmark="true" saveasname="MyReport.pdf">

If I explicitly declare the content disposition and content type, like so

<cfheader name="Content-Disposition" value="inline; filename=MyReport.pdf">
<cfcontent type="application/x-pdf">
<cfdocument format="pdf" bookmark="true" saveasname="MyReport.pdf">

  • Chrome tells me that "Content-Disposition" has been declared twice
  • Firefox tells me the PDF file is corrupt
  • IE just ignores it (and still doesn't show the right filename)

If I just rely on the header

<cfheader name="Content-Disposition" value="inline; filename=MyReport.pdf">
<cfcontent type="application/x-pdf">
<cfdocument format="pdf" bookmark="true">

I get the same behavior as the first snippet of code.

I know how to get the browser to prompt for download rather than displaying inline, and everything works as expected then, but that's not the desired behavior.

I need to use times and dates in filenames and the end users are not savvy enough to keep from overwriting their files (should they choose to save them).

Is there something I'm missing that will get IE and Firefox to do what they're supposed to? What other browsers are going to do this? Mobile Safari?

解决方案

The problem seems to be that "filename=xxx" was really intended for the "attachment" disposition, and not all the browser PDF plugins recognise it as a mechanism for specifying the inline "save as", as you've discovered.

A different approach to getting them all to use your preferred filename would be to manipulate the URL using web server rewrite rules. As a simple example you'd have your script for generating the pdfs and serving them inline: pdf.cfm

<cfheader name="Content-Disposition" value="inline">
<cfdocument format="PDF" mimetype="application/pdf">Test</cfdocument>

Then create a re-write rule which matches URLs in the form /pdf/myfilename and passes them to pdf.cfm. On IIS7 this might be:

<rule name="Inline PDF SaveAs" stopProcessing="true">
    <match url="^/pdf/[\w-]+$" ignoreCase="true" />
    <action type="Rewrite" url="/pdf.cfm" appendQueryString="false" />
</rule>

This will match filenames containing alphanumeric, underscore and hyphen characters only. You wouldn't want to allow spaces, or invalid filename characters.

When you access /pdf/myreport the PDF will be displayed inline by the plugin, and when you save it, the default filename will be myreport.pdf.

If you're using a framework which supports Search Engine Safe URLs or "routes", you could do the same without needing web server rewrites.

UPDATE: In fact you don't need to use URL rewriting: simply append a forward slash and then the desired filename to the CF script URL, e.g.

/pdf.cfm/myreport

The plugin will use whatever comes after the final slash as the "Save As..." name.

这篇关于在IE 9和Firefox 13中忽略了内容处置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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