2

How can i write file to temp directory in chrome filesystem api?

        chrome.fileSystem.getWritableEntry("temp dir here", function(entry) {
            var uniqid = Math.floor((Math.random() * 10) + 1)+Date.now();
            entry.getFile(uniqid+'.txt', {create:true}, function(entry) {
                entry.createWriter(function(writer) {
                    writer.write(new Blob([printText], {type: 'text/plain'}));
                });
            });
        });

I need this unique .txt to write to temp dir and then get that location..

4
  • 1
    What do you need it for? To write to the real filesystem you need the user's consent, you can't just pick a location. If you only need a temp file, you can do it with a virtual filesystem. Commented Dec 9, 2015 at 10:17
  • How can i do with virtual filesystem? I need to create file in temp folder, then copy to another folder. Complicated, but need it in that way :( Commented Dec 9, 2015 at 10:21
  • 1
    Question is, do you need it accessible to the user, or only inside the app? (also, I suppose extension tag does not apply, since it's an app-only API) Commented Dec 9, 2015 at 10:23
  • My app will create file to temp folder, and then copy it to second(my custom location), and then third-party software will print that to thermal printer, becouse it watch for new files in that second location and prints them. That is point. This is chrome app, not extension. Commented Dec 9, 2015 at 10:25

1 Answer 1

2

You can use the HTML5 virtual filesystem as your "staging" location before copying it to the final "real" folder. You'll still need to request access to the final folder.

Most details can be found in Exploring the FileSystem API article, but the idea is as follows: chrome.fileSystem API is built on the same principles, you just get different entries when you request them with chrome.fileSystem.chooseEntry

To get a virtual filesystem instead of a real one, you need a different call:

window.webkitRequestFileSystem(
  window.TEMPORARY,
  5*1024*1024 /* 5MB, adjust as needed, may require "unlimitedStorage" permission */,
  onInitFs,
  errorHandler
);

function onInitFs(filesystem) {
  /* do something with filesystem.root, which is a DirectoryEntry,
     just as you would with `chrome.fileSystem.chooseEntry` */
}

For more documentation, MDN is a good place.

Yes, there are big red scary warnings "do not use". It's not portable since only Chrome implements it and other browsers decided not to do that. But for writing specifically a Chrome App it's fine. If you need assurance that Chrome will continue to support it - well, chrome.fileSystem is tightly coupled with it.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.