Content-disposition being ignored in IE 9 and Firefox 13

a 夏天 提交于 2019-12-13 13:11:26

问题


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?


回答1:


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.




回答2:


Make sure you use ATTACHMENT and not INLINE.

IE does NOT like INLINE and will open a word document as READ ONLY as well as not assuming the filename you give it.

Example:

<cfheader name="content-disposition" value="attachment; filename=#filename#.doc" charset="utf-8">

NOT

<cfheader name="content-disposition" value="inline; filename=#filename#.doc" charset="utf-8">



回答3:


I would use the name attribute of the cfdocument tag which will store the contents in a variable. Then use the cfheader and cfcontent tags as you have above with the exception of replacing "inline;" with "attachment;"

I use code like this:

<cfdocument name="pdf"> ... </cfdocument>
<cfheader name="Content-Disposition" value="attachment;filename=MyReport.pdf;" />
<cfcontent type="application/pdf" variable="#pdf#" />


来源:https://stackoverflow.com/questions/11547469/content-disposition-being-ignored-in-ie-9-and-firefox-13

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!