Re: 关于JS API的疑问,我们可以用JavaScript让浏览器启动下载程序吗?

老帖頂上來
原文:http://lists.w3.org/Archives/Public/public-html-ig-zh/2011Apr
/thread#msg82

(11/04/28 13:51), Jinjiang Zhao wrote:
> 懂我者Hax也!
>
> 这个方法我最近几天也试出来了,不过还有一点体验上的瑕疵,可不可以指定要
> 保存的文件名呢?(在中文版chrome中会直接把文件名取为“下 载”二字且不带扩
> 展名)我没查到datauri是如何体现filename的……

最近 WHATWG 的 HTML5 規範加了 <a> 與 <area> 的 download 屬性[1],而
WebKit 也有這個的實作了[2],Hax 的範例可以直接加上該屬性,而本來的問題
(用 JavaScript 讓瀏覽器啟動下載程序)看似好像好像有以下的解法(目前
WebKit 的逐日建制應該可行,未來恐怕不可行):

var downloadAnchor = document.create('a');
downloadAnchor.download = <filename>;
download.click();

未來不可行的原因 — 規範提到 a 元素的啟動行為(activation behavior)第一段
[[If the click event in question is not trusted (i.e. a click() method
call was the reason for the event being dispatched), and either the a
element has a download attribute or the element's target attribute is
present and applying the rules for choosing a browsing context given a
browsing context name, using the value of the target attribute as the
browsing context name, would result in there not being a chosen browsing
context, then raise an INVALID_ACCESS_ERR exception and abort these steps.]]

也就是 document.createEvent 或是 click() 這些觸動應該都會變成
INCALID_ACCESS_ERR 而現在 WebKit 不合標準。有人有什麼好方法嗎?另外一個
根本的問題是,我們有對「用 JavaScript 讓瀏覽器啟動下載程序」有一定的需要
嗎?還是讓使用者按 <a> 就可以了?用 <a> 也可以

<a download="下載.pdf" href="http://www.example.com/下載"><span
id="download">下載</span></a>

#download { appearance: push-button; }

這裡用 <span> 而不用 <input>/<button> 的原因是,語意上 <a> 與
<input>/<button> 都表示一個指令(command)[3],而這裡只有一個指令而不是
兩個,如果用 <input>/<button> 的話 document.commands[4] 會多一個出
來。(不知道這種對 HTML5 指令的解釋對不對)

[1]
http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#attr-hyperlink-download
[2]
http://peter.sh/2011/08/web-intents-the-download-attribute-and-positioned-floats/
[3]
http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html
[4]
http://www.whatwg.org/specs/web-apps/current-work/multipage/commands.html#dom-document-commands

(11/04/28 13:15), John Hax wrote:
> 可以用data协议。附件是例子,在FF4下调试通过。

Hax 的方法在 Safari4 下也通過。


跟 download 有關的規範部份,「fetch as download」[5]算法的 12 步這裡有一
段緊告訊息:
[[If the algorithm reaches this step, then a download was begun from a
different origin than the resource being downloaded, and the origin did
not mark the file as suitable for downloading, and the download was not
initiated by the user. This could be because a download attribute was
used to trigger the download, or because the resource in question is not
of a type that the user agent supports.]]

這裡我覺得 ", and the download was not initiated by the user" 這邊很怪,
因為按一個有 download 的 <a> 不是算使用者啟動嗎?可能是加進 download 屬
性沒注意到的小的字句上的錯誤。

[5]
http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#as-a-download

(廣告:徵求有興趣翻譯 Peter Beverloo[6] 的 WebKit 新聞的朋友,最新的一
期就有提到 download 屬性跟 Web Intents。最簡單的方式就是自己在自己的網誌
上翻譯,然後把連結加到 wiki 裡(我只想把他的翻譯文蒐集完全),當然也非常
歡迎在 wiki 上直接翻譯。)

[6] http://www.w3.org/html/ig/zh/wiki/Peter_Beverloo


此致

呂 康豪(Kenny), 中文興趣小組W3C連絡人
推特: http://twitter.com/kanghaolu
Google+: https://plus.google.com/112088462407783855918/posts
新浪微博: http://t.sina.com.cn/1950042164

Received on Monday, 8 August 2011 06:41:05 UTC