How to choose the file name when user uses "save as"?
I have implemented a basic file upload/download on top of AppEngine datastore.
The functionality that I want is that a file named "base.c" will be opened as a text file. so I did the following: (of course my code is for a general case but this specific example illustrates the problem)resp.setContentType("text/plain");
resp.setHeader("Content-Disposition", "inline; filename=base.c");
This opens it as a text file in the browser which is just what I needed.
But I also want that when the user uses the browser's "save page as.." option it will offer the initial name of "base.c". It does just that in firefox(6) but it gives the default name of "download" on chrome(15) and safari(5). Even worse - when choosing base.c as the file name chrome warns the user that this is the wrong extension and asks for confirmation to continue. Now if I use "attachment" instead of "inline" it does not open the file but download it with the right name.What can I do to make the default save as.. name be "base.c"?
Is this the browser's fault or am I misusing the HTTP headers?(if it 开发者_如何学Cis any help, all test were on OSX 10.6 and the download site is of the sort:
http://------.appspot.com/download?name=base.c)I think most browsers default the page name as the current page for the save as option.
One option may be to change the URL from appspot/download?name=base.c to appspot.com/download/base.c by making use of the url-pattern in web.xml.
e.g.
<servlet-mapping>
<servlet-name>download</servlet-name>
<url-pattern>/download/*</url-pattern>
</servlet-mapping>
then parse the URL to get the filename from the datastore.
I apologize if this won't work with GAE, I am not familiar with it.
This is a known issue; see http://greenbytes.de/tech/tc2231/#inlwithasciifilename -- only FF gets this right.
I am not sure if this is an actual answer but this might solve your problem. you can give the user the option to choose between a preview and a download. this will solve your problem
You shouldn't expect the filename
argument to Content-Disposition
to be respected for any value other than attachment
. Your best option, as @scott suggests, is to modify your URL mapping so that the URL path ends with the filename you want; this is also friendlier for users. Another option you could use in conjunction would be to show the file formatted inside an HTML page, with a 'raw' download link that sends Content-Disposition: attachment
, just like GitHub does.
精彩评论