Using HTML5/JavaScript to generate and save a file

后端 未结 17 1723
有刺的猬
有刺的猬 2020-11-21 07:30

I\'ve been fiddling with WebGL lately, and have gotten a Collada reader working. Problem is it\'s pretty slow (Collada is a very verbose format), so I\'m going to start conv

17条回答
  •  余生分开走
    2020-11-21 07:56

    As previously mentioned the File API, along with the FileWriter and FileSystem APIs can be used to store files on a client's machine from the context of a browser tab/window.

    However, there are several things pertaining to latter two APIs which you should be aware of:

    • Implementations of the APIs currently exist only in Chromium-based browsers (Chrome & Opera)
    • Both of the APIs were taken off of the W3C standards track on April 24, 2014, and as of now are proprietary
    • Removal of the (now proprietary) APIs from implementing browsers in the future is a possibility
    • A sandbox (a location on disk outside of which files can produce no effect) is used to store the files created with the APIs
    • A virtual file system (a directory structure which does not necessarily exist on disk in the same form that it does when accessed from within the browser) is used represent the files created with the APIs

    Here are simple examples of how the APIs are used, directly and indirectly, in tandem to do this:

    BakedGoods*

    bakedGoods.get({
            data: ["testFile"],
            storageTypes: ["fileSystem"],
            options: {fileSystem:{storageType: Window.PERSISTENT}},
            complete: function(resultDataObj, byStorageTypeErrorObj){}
    });
    

    Using the raw File, FileWriter, and FileSystem APIs

    function onQuotaRequestSuccess(grantedQuota)
    {
    
        function saveFile(directoryEntry)
        {
    
            function createFileWriter(fileEntry)
            {
    
                function write(fileWriter)
                {
                    var dataBlob = new Blob(["Hello world!"], {type: "text/plain"});
                    fileWriter.write(dataBlob);              
                }
    
                fileEntry.createWriter(write);
            }
    
            directoryEntry.getFile(
                "testFile", 
                {create: true, exclusive: true},
                createFileWriter
            );
        }
    
        requestFileSystem(Window.PERSISTENT, grantedQuota, saveFile);
    }
    
    var desiredQuota = 1024 * 1024 * 1024;
    var quotaManagementObj = navigator.webkitPersistentStorage;
    quotaManagementObj.requestQuota(desiredQuota, onQuotaRequestSuccess);
    

    Though the FileSystem and FileWriter APIs are no longer on the standards track, their use can be justified in some cases, in my opinion, because:

    • Renewed interest from the un-implementing browser vendors may place them right back on it
    • Market penetration of implementing (Chromium-based) browsers is high
    • Google (the main contributer to Chromium) has not given and end-of-life date to the APIs

    Whether "some cases" encompasses your own, however, is for you to decide.

    *BakedGoods is maintained by none other than this guy right here :)

提交回复
热议问题