Can I write files with HTML5/JS?

后端 未结 6 964
时光说笑
时光说笑 2020-11-30 05:13

I wonder if there is any way I can write to files from HTML5/JS? In the broswer ...

6条回答
  •  被撕碎了的回忆
    2020-11-30 05:39

    The answer to this question depends on your answers to the following questions:

    • Are you fine with the fact that support for such a capability currently exists only in Chromium-based browsers (Chrome & Opera)?
    • Are you fine with utilizing an as-of-now proprietary API to take advantage of such a capbility?
    • Are you fine with the possibility of removal of said API in the future?
    • Are you fine with the constriction of files created with said API to a sandbox (a location outside of which the files can produce no effect) on disk?

    If you answered "yes" to all of the above, then yes, with the FileWriter and FileSystem APIs, you can write files from the context of a browser tab/window using Javascript.

    Here is a simple example of how the APIs are used in tandem to do this:

    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);
    

    With BakedGoods*, a Javascript library that establishes a uniform interface that can be used to conduct common storage operations in all native (including FileSystem), and some non-native storage facilities, the above is accomplished with this:

    bakedGoods.set({
        data: [{key: "testFile", value: "Hello world!", type: "text/plain"}],
        storageTypes: ["fileSystem"],
        options: {fileSystem:{storageType: Window.PERSISTENT}},
        complete: function(byStorageTypeStoredItemRangeDataObj, byStorageTypeErrorObj){}
    });
    

    The FileSystem spec defines no guidelines on how directory structures are to appear on disk. In Chromium-based browsers for example, the sandbox has 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), within which the directories and files created with the APIs are placed.

    So though you may be able to write files to a system with the APIs, locating the files without the APIs (well, without the FileSystem API) could be a non-trivial affair.

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

提交回复
热议问题