Code Working Fine on jsFiddle but not on local system

This code works fine on jsFiddle, but not on my system. Jsfiddle

I checked from the draft (by pressing Ctrl + Shift + Enter on jsFiddle), adding this code to the header section and changing it as shown 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
source share
2 answers

You get FileError.SECURITY_ERR because you are not allowed to run this code locally. You will see an error if you did not have empty error handlers.

You will see an error if you save the following code in 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> 
+4
source

In the local system

Google Chrome has some limitations for file:// . You can try running Chrome with --allow-file-access-from-files . Maybe this will help. Otherwise, you need to put the web page on some development server in order to check it.

+1
source

Source: https://habr.com/ru/post/898841/


All Articles