6

This code is working fine on jsFiddle but not on my system. JsFiddle

I have checked from the draft (Pressing Ctrl + Shift + Enter on jsFiddle), added this code to head section & modified like below:

window.addEvent('load', function() {
    window.webkitRequestFileSystem(window.TEMPORARY, 2*1024*1024, function(fs) {
        fs.root.getFile('test', {create: true}, function(fileEntry) {
            alert(fileEntry.toURL());
            fileEntry.createWriter(function(fileWriter) {
                var builder = new WebKitBlobBuilder();
                builder.append("Saurabh");
                builder.append("\n");
                builder.append("Saxena");

                var blob = builder.getBlob('text/plain');

                fileWriter.onwriteend = function() {
                    // navigate to file, will download
                    location.href = fileEntry.toURL();
                };
                fileWriter.write(blob);
            }, function() {});
        }, function() {});
    }, function() {});
});
6
  • 2
    I'm not sure I agree with what just happened when I clicked that link... Commented Oct 7, 2011 at 15:26
  • 1
    The file is created in the TEMPORARY storage and gets downloaded with the written contents. Commented Oct 7, 2011 at 15:27
  • Guys, this is not a malicious code, this code will not harm your system. Commented Oct 7, 2011 at 15:33
  • What the heck? How did you get a file to automatically enter my downloads folder? It's not malicious.... but it does seem like a huge security hole. Chrome 13 here. Commented Oct 7, 2011 at 15:35
  • Please guys, help me out. This is really very urgent. Commented Oct 7, 2011 at 15:40

2 Answers 2

4

You get a FileError.SECURITY_ERR because you are not allowed to run this code locally. You would see the error if you didn't have empty errorhandlers.

You will see the error if you save the following code to a local file and run it in chrome:

<html>
<script>
function doit() {

function errorHandler(e) {
  var msg = '';

  switch (e.code) {
    case FileError.QUOTA_EXCEEDED_ERR:
      msg = 'QUOTA_EXCEEDED_ERR';
      break;
    case FileError.NOT_FOUND_ERR:
      msg = 'NOT_FOUND_ERR';
      break;
    case FileError.SECURITY_ERR:
      msg = 'SECURITY_ERR';
      break;
    case FileError.INVALID_MODIFICATION_ERR:
      msg = 'INVALID_MODIFICATION_ERR';
      break;
    case FileError.INVALID_STATE_ERR:
      msg = 'INVALID_STATE_ERR';
      break;
    default:
      msg = 'Unknown Error';
      break;
  };

  console.log('Error: ' + msg);
}



window.webkitRequestFileSystem(window.TEMPORARY, 1024*1024, function(fs) {
        fs.root.getFile('test', {create: true}, function(fileEntry) {
            fileEntry.createWriter(function(fileWriter) {
                var builder = new WebKitBlobBuilder();
                builder.append("Saurabh");
                builder.append("\n");
                builder.append("Saxena");

                var blob = builder.getBlob('text/plain');

                fileWriter.onwriteend = function() {
                    // navigate to file, will download
                    location.href = fileEntry.toURL();
                };
                fileWriter.write(blob);
            }, errorHandler);
        }, errorHandler);
    }, errorHandler);
}
</script>

<body onload="doit();">

</body>
</html>
Sign up to request clarification or add additional context in comments.

6 Comments

you are right so, what could be the possible solution for this. is there any way by which i can get permission to access files from the user itself.
As far as i know, there is no way of making this work with a file:// url unless you run the code as a chrome extension --allow-file-access-from-files will not help you. Run it remotely and it will work, or build a chrome extension for it and run it as a privileged code.
I can build a chrome extension, but are you sure that same code will run there also
When you run your code inside a chrome extension it has raised privileges compared to running it on a website.
Added this code to the Event handler of chrome.browserAction.onClicked.addListener(createFile); function createFile() { //same code } not working ...... :(
|
1

On Local System

Google Chrome have some restrictions for file://. You can try to run Chrome with --allow-file-access-from-files. Maybe it will help. Otherwise you need to put web page on some development server to test it.

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.