用户使用“另存为”时如何选择文件名?
我已经在 AppEngine 数据存储区上实现了基本的文件上传/下载。
我想要的功能是将名为“base.c”的文件作为文本文件打开。 所以我做了以下事情:
(当然,我的代码适用于一般情况,但这个具体示例说明了问题)
resp.setContentType("text/plain");
resp.setHeader("Content-Disposition", "inline; filename=base.c");
这将其作为浏览器中的文本文件打开,这正是我所需要的。
但我还希望当用户使用浏览器的“将页面另存为..”选项时,它将提供初始名称“base.c”。 它在 Firefox 中就是这样做的(6),但它在 chrome(15) 和 safari(5) 上给出默认名称“download”。
更糟糕的是 - 当选择 base.c 作为文件名时,chrome 会警告用户这是错误的扩展名,并要求确认才能继续。
现在,如果我使用“附件”而不是“内联”,它不会打开文件,而是使用正确名称下载它。
我该怎么做才能使默认另存为..名称为“base.c”?
这是浏览器的错误还是我滥用了 HTTP 标头?
(如果有任何帮助,所有测试均在 OSX 10.6 上进行,下载站点如下:
http://-----.appspot.com/download ?name=base.c)
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 is any help, all test were on OSX 10.6 and the download site is of the sort:
http://------.appspot.com/download?name=base.c)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为大多数浏览器默认将页面名称作为“另存为”选项的当前页面。
一种选择可能是通过使用 web.xml 中的 url-pattern 将 URL 从 appspot/download?name=base.c 更改为 appspot.com/download/base.c。
例如
,然后解析 URL 以从数据存储中获取文件名。
如果这不适用于 GAE,我很抱歉,我对它不熟悉。
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.
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.
这是一个已知问题;参见 http://greenbytes.de/tech/tc2231/#inlwithasciifilename -- 只有 FF 获得这对。
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
您不应期望
Content-Disposition
的filename
参数会受到除attachment
之外的任何值的影响。正如 @scott 建议的那样,最好的选择是修改 URL 映射,使 URL 路径以您想要的文件名结尾;这对于用户来说也更加友好。您可以结合使用的另一个选项是显示 HTML 页面内格式化的文件,并使用发送Content-Disposition:attachment
的“原始”下载链接,就像 GitHub 所做的那样。You shouldn't expect the
filename
argument toContent-Disposition
to be respected for any value other thanattachment
. 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 sendsContent-Disposition: attachment
, just like GitHub does.