Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code Working Fine on jsFiddle but not on Local System

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() {});
});
like image 669
Saurabh Saxena Avatar asked Oct 07 '11 15:10

Saurabh Saxena


2 Answers

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>
like image 158
Martin Jespersen Avatar answered Nov 09 '22 07:11

Martin Jespersen


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.

like image 28
Andrey M. Avatar answered Nov 09 '22 09:11

Andrey M.