Is there any way to specify a suggested filename when using data: URI?

前端 未结 16 1673
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 03:09

If for example you follow the link:

data:application/octet-stream;base64,SGVsbG8=

The browser will prompt you to download a file consisting of t

16条回答
  •  灰色年华
    2020-11-22 03:45

    I've looked a bit in firefox sources in netwerk/protocol/data/nsDataHandler.cpp

    data handler only parses content/type and charset, and looks if there is ";base64" in the string

    the rfc specifices no filename and at least firefox handles no filename for it, the code generates a random name plus ".part"

    I've also checked firefox log

    [b2e140]: DOCSHELL 6e5ae00 InternalLoad data:application/octet-stream;base64,SGVsbG8=
    [b2e140]: Found extension '' (filename is '', handling attachment: 0)
    [b2e140]: HelperAppService::DoContent: mime 'application/octet-stream', extension ''
    [b2e140]: Getting mimeinfo from type 'application/octet-stream' ext ''
    [b2e140]: Extension lookup on '' found: 0x0
    [b2e140]: Ext. lookup for '' found 0x0
    [b2e140]: OS gave back 0x43609a0 - found: 0
    [b2e140]: Searched extras (by type), rv 0x80004005
    [b2e140]: MIME Info Summary: Type 'application/octet-stream', Primary Ext ''
    [b2e140]: Type/Ext lookup found 0x43609a0
    

    interesting files if you want to look at mozilla sources:

    data uri handler: netwerk/protocol/data/nsDataHandler.cpp
    where mozilla decides the filename: uriloader/exthandler/nsExternalHelperAppService.cpp
    InternalLoad string in the log: docshell/base/nsDocShell.cpp
    

    I think you can stop searching a solution for now, because I suspect there is none :)

    as noticed in this thread html5 has download attribute, it works also on firefox 20 http://www.whatwg.org/specs/web-apps/current-work/multipage/links.html#attr-hyperlink-download

提交回复
热议问题