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

前端 未结 16 1651
佛祖请我去吃肉
佛祖请我去吃肉 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:37

    Use the download attribute:

    <a download='FileName' href='your_url'>
    

    Live example on html5-demos.appspot.com/....

    The download attribute works on Chrome, Firefox, Edge, Opera, desktop Safari 10+, iOS Safari 13+, and not IE11.

    0 讨论(0)
  • 2020-11-22 03:38

    Using service workers, this is finally possible in the truest sense.

    1. Create a fake URL. For example /saveAs/myPrettyName.jpg
    2. Use URL in <a href, <img src, window.open( url ), absolutely anything that can be done with a "real" URL.
    3. Inside the worker, catch the fetch event, and respond with the correct data.

    The browser will now suggest myPrettyName.jpg even if the user opens the file in a new tab, and tries to save it there. It will be exactly as if the file had come from the server.

    // In the service worker
    self.addEventListener( 'fetch', function(e)
    {
        if( e.request.url.startsWith( '/blobUri/' ) )
        {
            // Logic to select correct dataUri, and return it as a Response
            e.respondWith( dataURLAsRequest );
        }
    });
    
    0 讨论(0)
  • 2020-11-22 03:44
    var isIE = /*@cc_on!@*/false || !!document.documentMode; // At least IE6
    var sessionId ='\n';
    var token = '\n';
    var caseId = CaseIDNumber + '\n';
    var url = casewebUrl+'\n';
    var uri = sessionId + token + caseId + url;//data in file
    var fileName = "file.i4cvf";// any file name with any extension
    if (isIE)
        {
                var fileData = ['\ufeff' + uri];
                var blobObject = new Blob(fileData);
                window.navigator.msSaveOrOpenBlob(blobObject, fileName);
        }
        else //chrome
        {
            window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
             window.requestFileSystem(window.TEMPORARY, 1024 * 1024, function (fs) {
                fs.root.getFile(fileName, { create: true }, function (fileEntry) { 
                    fileEntry.createWriter(function (fileWriter) {
                        var fileData = ['\ufeff' + uri];
                        var blob = new Blob(fileData);
                        fileWriter.addEventListener("writeend", function () {
                            var fileUrl = fileEntry.toURL();
                            var link = document.createElement('a');
                            link.href = fileUrl;
                            link.download = fileName;
                            document.body.appendChild(link);
                            link.click();
                            document.body.removeChild(link);
                        }, false);
                        fileWriter.write(blob);
                    }, function () { });
                }, function () { });
             }, function () { });
        }
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-22 03:46

    It's kind of hackish, but I've been in the same situation before. I was dynamically generating a text file in javascript and wanted to provide it for download by encoding it with the data-URI.

    This is possible with minormajor user intervention. Generate a link <a href="data:...">right-click me and select "Save Link As..." and save as "example.txt"</a>. As I said, this is inelegant, but it works if you do not need a professional solution.

    This could be made less painful by using flash to copy the name into the clipboard first. Of course if you let yourself use Flash or Java (now with less and less browser support I think?), you could probably find a another way to do this.

    0 讨论(0)
  • 2020-11-22 03:50

    Look at this link: http://lists.w3.org/Archives/Public/uri/2010Feb/0069.html

    Quote:

    It even works (as in, doesn't cause a problem) with ;base64 at the end
    like this (in Opera at least):

    data:text/plain;charset=utf-8;headers=Content-Disposition%3A%20attachment%3B%20filename%3D%22with%20spaces.txt%22%0D%0AContent-Language%3A%20en;base64,4oiaDQo%3D

    Also there is some info in the rest messages of the discussion.

    0 讨论(0)
提交回复
热议问题