Local file access with JavaScript

前端 未结 13 2545
悲哀的现实
悲哀的现实 2020-11-21 04:07

Is there local file manipulation that\'s been done with JavaScript? I\'m looking for a solution that can be accomplished with no install footprint like requiring Adobe AIR.<

相关标签:
13条回答
  • 2020-11-21 04:59

    Just an update of the HTML5 features is in http://www.html5rocks.com/en/tutorials/file/dndfiles/. This excellent article will explain in detail the local file access in JavaScript. Summary from the mentioned article:

    The specification provides several interfaces for accessing files from a 'local' filesystem:

    1. File - an individual file; provides readonly information such as name, file size, MIME type, and a reference to the file handle.
    2. FileList - an array-like sequence of File objects. (Think <input type="file" multiple> or dragging a directory of files from the desktop).
    3. Blob - Allows for slicing a file into byte ranges.

    See Paul D. Waite's comment below.

    0 讨论(0)
  • 2020-11-21 05:01

    If you're deploying on Windows, the Windows Script Host offers a very useful JScript API to the file system and other local resources. Incorporating WSH scripts into a local web application may not be as elegant as you might wish, however.

    0 讨论(0)
  • 2020-11-21 05:02

    If you need access to the entire file system on the client, read/write files, watch folders for changes, start applications, encrypt or sign documents, etc. please have a look at JSFS.

    It allows secure and unlimited access from your web page to computer resources on the client without using a browser plugin technology like AcitveX or Java Applet. However, a peace of software has to be installed too.

    In order to work with JSFS you should have basic knowledge in Java and Java EE development (Servlets).

    Please find JSFS here: https://github.com/jsfsproject/jsfs. It's free and licensed under GPL

    0 讨论(0)
  • 2020-11-21 05:06

    UPDATE This feature is removed since Firefox 17 (see https://bugzilla.mozilla.org/show_bug.cgi?id=546848).


    On Firefox you (the programmer) can do this from within a JavaScript file:

    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
    netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserWrite");
    

    and you (the browser user) will be prompted to allow access. (for Firefox you just need to do this once every time the browser is started)

    If the browser user is someone else, they have to grant permission.

    0 讨论(0)
  • 2020-11-21 05:06

    There is a (commercial) product, "localFS" which can be used to read and write entire file-system on client computer.

    Small Windows app must be installed and tiny .js file included in your page.

    As a security feature, file-system access can be limited to one folder and protected with a secret-key.

    https://www.fathsoft.com/localfs

    0 讨论(0)
  • 2020-11-21 05:11

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

    There are several things pertaining to the FileSystem and FileWriter APIs which you should be aware of, some of which were mentioned, but are worth repeating:

    • 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 these things:

    BakedGoods*

    Write file:

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

    Read file:

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

    Using the raw File, FileWriter, and FileSystem APIs

    Write file:

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

    Read file:

    function onQuotaRequestSuccess(grantedQuota)
    {
    
        function getfile(directoryEntry)
        {
    
            function readFile(fileEntry)
            {
    
                function read(file)
                {
                    var fileReader = new FileReader();
    
                    fileReader.onload = function(){var fileData = fileReader.result};
                    fileReader.readAsText(file);             
                }
    
                fileEntry.file(read);
            }
    
            directoryEntry.getFile(
                "testFile", 
                {create: false},
                readFile
            );
        }
    
        requestFileSystem(Window.PERSISTENT, grantedQuota, getFile);
    }
    
    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 :)

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