Stefan Lennerbrant
asked on
Making clients use the proper filename on web file output
I've got a web server script named http://server/scripts/getpdf.exe that outputs a PDF file to the client browser.
The script adds a "filename" to the http headers, to make sure that the actual PDF file name is used by the client, when users then saves the file to local disk etc
However, many browsers/AdobeAcrobatReade r etc, do not use this filename - instead it proposes to name the file "getpdf.exe" normally with a small change by adding a PDF extension into "getpdf.pdf" or "getpdf.exe.pdf" (different behaviour on different clients)
However, I would like the "filename" attribute to be used as local file name.
How should the headers look like to make the clients understand that the filename to use is "myfile.pdf", even though the link is named /scripts/getpdf.exe
Today I use the following (captured with HttpWatch)
Status:
HTTP/1.1 200 OK
Headers:
Connection: close
Content-Disposition: filename="myfile.pdf"
Content-Type: application/pdf
Date: Wed, 20 Feb 2013 08:32:14 GMT
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
The script adds a "filename" to the http headers, to make sure that the actual PDF file name is used by the client, when users then saves the file to local disk etc
However, many browsers/AdobeAcrobatReade
However, I would like the "filename" attribute to be used as local file name.
How should the headers look like to make the clients understand that the filename to use is "myfile.pdf", even though the link is named /scripts/getpdf.exe
Today I use the following (captured with HttpWatch)
Status:
HTTP/1.1 200 OK
Headers:
Connection: close
Content-Disposition: filename="myfile.pdf"
Content-Type: application/pdf
Date: Wed, 20 Feb 2013 08:32:14 GMT
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
The only alternative I know to 'attachment' is 'inline' which means the file is shown in the browser. I'm not sure how the filename is relevant in that case. The only situation I'm aware of where you want to force the filename to something else then the page url is exactly when the file is to be saved locally by the user. In short, I don't understand your situation but still I can only think of this solution ;-) The Content-Type that you suggest may be some sort of hack but seems ridiculous to me otherwise.
ASKER
The reason for not using "attachment" but still wanting a real file name, is that the user shall get the document displayed as conventiently as possible, without the prompt "open or save"
At that point, when viewing the document, the user may select "save as" in the client program, i.e. IE or Acrobat Reader etc. Then the "filename" attribute should be proposed.
So the use of "filename" without "attachment" is quite handy.
With "attachment" it all seems to work quite OK, but the irritating "open or save" prompt is displayed, of course.
I would however have guessed that clients should respect the "filename" attribute even though "attachment" is not specified?
I cannot find any info in the specs on why not?
Also, I just recently discovered a critical problem with not respecting "filename" in this manner.
When a PDF is displayed in the IE browser, the user has the possiblity to "share via mail" (or similar term/text).
Then Outlook (or similar) is opened with the PDF attached to an empty message.
However, the filename is then given as "getpdf.exe" (not even using the PDF extension) and Outlook will then block the file as unsecure. Even though it is, of course, a PDF file.
With "attachment" the real filename is used instead.
So... attachment seems to be the solution.
However, then the prompt open/save pops up all the time.
So the real question is -- why is "filename" not respected by clients? Why on earth would the URL name be used when a filename is specified in the headers?
Sigh! :-)
At that point, when viewing the document, the user may select "save as" in the client program, i.e. IE or Acrobat Reader etc. Then the "filename" attribute should be proposed.
So the use of "filename" without "attachment" is quite handy.
With "attachment" it all seems to work quite OK, but the irritating "open or save" prompt is displayed, of course.
I would however have guessed that clients should respect the "filename" attribute even though "attachment" is not specified?
I cannot find any info in the specs on why not?
Also, I just recently discovered a critical problem with not respecting "filename" in this manner.
When a PDF is displayed in the IE browser, the user has the possiblity to "share via mail" (or similar term/text).
Then Outlook (or similar) is opened with the PDF attached to an empty message.
However, the filename is then given as "getpdf.exe" (not even using the PDF extension) and Outlook will then block the file as unsecure. Even though it is, of course, a PDF file.
With "attachment" the real filename is used instead.
So... attachment seems to be the solution.
However, then the prompt open/save pops up all the time.
So the real question is -- why is "filename" not respected by clients? Why on earth would the URL name be used when a filename is specified in the headers?
Sigh! :-)
It might just be that the 'syntax' of the header needs the disposition before the filename. Did you try:
Content-Disposition: inline; filename="myfile.pdf"
Also: I have seen problems with this before, it had to do with the quotes I think although these _are_ part of the standard. If you are not using this standard filename (myfile.pdf) but a real filename it might be worth exploring other options like urlencoding the filename instead of using quotes.
ASKER
I have now tried with:
Content-Disposition: filename="myfile.pdf"
Content-Disposition: inline; filename="myfile.pdf"
Content-Disposition: inline; filename=myfile.pdf
and with all three methods, the filename used by the client is based on "getpdf" instead of basing it on "myfile"
:-(
Content-Disposition: filename="myfile.pdf"
Content-Disposition: inline; filename="myfile.pdf"
Content-Disposition: inline; filename=myfile.pdf
and with all three methods, the filename used by the client is based on "getpdf" instead of basing it on "myfile"
:-(
ASKER
Increasing points to 500
Can you confirm this is just in Safari or all around?
I have found some ol ASP code that does a Server.URLEncode on the filename instead of using quotes for IE (old version probably).
Just to check this off, could you try:
I have found some ol ASP code that does a Server.URLEncode on the filename instead of using quotes for IE (old version probably).
Just to check this off, could you try:
Content-Disposition: inline; filename=myfile%2Epdf
ASKER
Thanks for the suggestions.
However, using
Content-Disposition: inline; filename=myfile%2Epdf
is equally faulted as the other three, not using the filename at all.
However, as soon as I switch to "attachment", it works ok -- but with that "open or save" prompt. Also, I think that depending on browser/Windows setting, I may run into cases when I get a "save" option only, and no automatic viewing at all.
I have not tested with Safari (I hope I didn't say that?) but mainly with IE9.
In IE9, when using the "save" icon in the browser window displaying the resulting PDF, the filename is proposed to be "getpdf.pdf"
When instead using the "mail as copy" icon in the browser window, the filename (attached into Outlook) is proposed to be "getpdf.exe.pdf"
Sigh!
However, using
Content-Disposition: inline; filename=myfile%2Epdf
is equally faulted as the other three, not using the filename at all.
However, as soon as I switch to "attachment", it works ok -- but with that "open or save" prompt. Also, I think that depending on browser/Windows setting, I may run into cases when I get a "save" option only, and no automatic viewing at all.
I have not tested with Safari (I hope I didn't say that?) but mainly with IE9.
In IE9, when using the "save" icon in the browser window displaying the resulting PDF, the filename is proposed to be "getpdf.pdf"
When instead using the "mail as copy" icon in the browser window, the filename (attached into Outlook) is proposed to be "getpdf.exe.pdf"
Sigh!
Well, that URLEncoded option was probably only for IE7 or something.
There is of course one completely other way of doing this: save the file to disk and redirect to it. Was that already considered? You would probably have to schedule a cleanup script though (or fiddle with session events).
I have not tested with Safari (I hope I didn't say that?)Oops, no you didn't, that was just me...
There is of course one completely other way of doing this: save the file to disk and redirect to it. Was that already considered? You would probably have to schedule a cleanup script though (or fiddle with session events).
ASKER
Using "attachment" is not the actual answer to the question, which is: how to make browsers respect the specified "filename" in a non-attachment (inline) Content-Disposition.
However, it solves the acute problem. It introduces a different (not good) behaviour, but still it is better than the alternative.
However, it solves the acute problem. It introduces a different (not good) behaviour, but still it is better than the alternative.
ASKER
That might very well be the solution to the matter -- I'm testing a little.
Of course, attachment makes the client prompt for "open or save file" - that's probably OK but a little irritating.
But why would the file name (filename=xxx.pdf) not be respected when not using attachment? Any explanation?
An alternative guess would be to use both
Content-Disposition: filename="xxx.pdf"
Content-Type: name="xxx.pdf"
The latter is non-standard (as I understand it) but may be interpreted by some clients.
I however haven't been able to test that yet. Any ideas on whether that may work better with some clients?
I'll return with info on whether attachment seems to completely solve the issue. It's the best guess right now.