34

I've seen some posts regarding access to files on a client machine by a webpage, namely this question.

I'm trying to hop on the "continuously update in the cloud" paradigm for some algorithms I am writing so my users can access the latest versions by simply accessing the webpage. This requires that the program/webpage can start with a directory and recursively inspect files within it and compute results based on what is found. In the end it also should be able to write the results file to the client's filesystem.

One of the answers in that previous question mentions Google Gears but that has since been discontinued in favor of HTML5. Is access to a client directory possible within HTML5? How?

I know why access by any webpage to local files is a security risk, but for my purpose I have no problem to ask the user for the appropriate permissions.

Community
  • 1
  • 1
greye
  • 8,921
  • 12
  • 41
  • 46
  • How did you go with this in the end? I'm doing the same analysis on browser apps with full file system access. Thanks – Dave Feb 08 '11 at 19:36

3 Answers3

31

No, not directly at least. However, you have a number of choices here.

Currently your best choices are:

  • Drag and drop files from desktop, see a tutorial. (Link disabled for malware/phishing)
  • Use input type file.
    • Read the contents with the File API or submit the form. Read more on Mozilla Developer Center about reading the file dynamically.
    • You can specify multiple attribute to read and open multiple files at once without having to have separate fields.
    • You can have an invisible input and "trigger a click" on it to open the file open dialog. See the previous Mozilla Developer Center link for details.
  • Use the FileSystem API which allows you to create, delete, read, modify files on the file system. Note: you get a sandboxed directory to work with, you can't access the whole system just like that.
  • Use Java with signed applets to access the whole file system. This requires the user to accept the signature.
fcdt
  • 2,371
  • 5
  • 14
  • 26
Tower
  • 98,741
  • 129
  • 357
  • 507
  • Hi Kai, do you know if your above statement that HTML 5 doesn't allow full file system access still holds true? Trying to find a good solution for either in browser or out of browser file system access. Ideally the solution would be using web dev technologies like HTML 5/Silverlight/Adobe Air. Thanks, Dave – Dave Feb 08 '11 at 19:39
  • 2
    @Dave: As of now (10th Feb 2011) and in the foreseeable future, HTML5 will not provide read/write access to the user's filesystem directly. There is something spec'd and implemented in Chrome (http://dev.w3.org/2009/dap/file-system/pub/FileSystem/), but it only lets you run actions in a sandboxed directory with no access to the external files. I think Java has the greatest power allowing you write/read pretty much any files on the system if you first Sign the applet. The user will be obviously asked to trust the signature. – Tower Feb 10 '11 at 18:31
  • In April 2014, it was announced on public-webapps that the Filesystem API spec is not being considered by other browsers. For now, the API is Chrome-specific and it's unlikely to be implemented by other browsers and is no longer being standardized with the W3C. src: http://www.html5rocks.com/en/tutorials/file/filesystem/ – Facundo Colombier Jun 29 '16 at 20:22
2

Chrome 6 also will support the File API

Vanuan
  • 31,770
  • 10
  • 98
  • 102
1

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

Given the current statuses the FileSystem and FileWriter APIs, their utilization to read and write files currently does not constitute an "HTML5 way" of doing those things.

Renewed interest in the APIs from the un-implementing browser vendors may place them right back on the standards track, though. That, combined with the high market penetration of Chromium-based browsers and the fact that Google (the main contributer to Chromium) has not given and end-of-life date to the APIs should be enough to justify their use in some cases.

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

Kevin
  • 2,617
  • 29
  • 35