Note that there are some explanatory texts on larger screens.

plurals
  1. PODownload file from FileSystem in Google Chrome
    primarykey
    data
    text
    <p>I've created an app for Google Chrome that stores a file into the File System. I can read this file but every time I try to export it, it doesn't work. I've tried some methods:</p> <ul> <li>method toUrl, </li> <li><blockquote> <p><a href="https://stackoverflow.com/questions/3749231/download-file-using-javascript-jquery">Download File Using Javascript/jQuery</a></p> </blockquote></li> <li><blockquote> <p><a href="https://stackoverflow.com/questions/7160720/create-a-file-using-javascript-in-chrome-on-client-side">create a file using javascript in chrome on client side</a></p> </blockquote></li> </ul> <p>but they doesn't work.</p> <p><a href="https://stackoverflow.com/questions/16402696/replacement-for-fileentry-tourl-in-chrome-packaged-apps">Replacement for fileEntry.toURL() in Chrome Packaged Apps</a> talks about my problem.. So I changed my code into</p> <pre><code>function readFileRdf() { window.requestFileSystem(window.PERSISTENT, 5*1024*1024, function(filesystem) { fs = filesystem; fs.root.getFile('rdf.txt', {create: false}, function(fileEntry) { // Get a File object representing the file, // then use FileReader to read its contents. fileEntry.file(function(file) { var reader = new FileReader(); reader.onloadend = function(e) { fromFileSystemRdf = e.target.result; arr = fromFileSystemRdf; exportRdf(arr); }; reader.readAsText(file); }, errorHandler); }, errorHandler); }); } </code></pre> <p>and </p> <pre><code>function exportRdf(arr){ console.log(arr); chrome.fileSystem.chooseEntry({type: 'saveFile'}, function(writableFileEntry) { writableFileEntry.createWriter(function(writer) { writer.onerror = errorHandler; writer.onwriteend = function(e) { console.log('write complete'); }; console.log(arr); writer.write(new Blob([arr], {type: 'text/plain'})); }, errorHandler); }); } </code></pre> <p>the last problem is that I get an error with <strong>createWriter</strong></p> <p><em>Error in response to fileSystem.chooseEntry: TypeError: Cannot call method 'createWriter' of undefined</em></p> <p>Up.. adding a console.log(chrome.runtime.lastError); it says</p> <p><em>Object {message: "Invalid calling page. This function can't be called from a background page."}</em> </p> <p>Question solved. Since is not possible to call a method from a background page, I've used this solution: (Remember, Google Chrome doesn't accept inline javascript)</p> <p>button for exporting is pressed</p> <pre><code>`document.getElementById("button2").addEventListener("click",readFileRdf);` </code></pre> <p>method readFileRdf creates a new page:</p> <pre><code>function readFileRdf() { chrome.app.window.create("data.html"); } </code></pre> <p><strong>data.html</strong> calls the data.js file</p> <pre><code> &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;meta charset="utf-8"&gt; &lt;title&gt;Data page&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;/body&gt; &lt;script rel="text/javascript" src="data.js"&gt;&lt;/script&gt; &lt;/html&gt; </code></pre> <p>and <strong>data.js</strong> allows the user to read the file from filesystem and download it </p> <pre><code>window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem; function initFs() { window.requestFileSystem(window.PERSISTENT, 5*1024*1024, function(filesystem) { fs = filesystem; readFile(fs); }, errorHandler); } function readFile(fs) { fs.root.getFile('rdf.txt', {create: false}, function(fileEntry) { // Get a File object representing the file, // then use FileReader to read its contents. fileEntry.file(function(file) { var reader = new FileReader(); reader.onloadend = function(e) { fromFileSystemRdf = e.target.result; arr = fromFileSystemRdf; exportRdf(arr); }; reader.readAsText(file); }, errorHandler); }, errorHandler); } function exportRdf(arr){ console.log(arr); chrome.fileSystem.chooseEntry({type: 'saveFile'}, function(writableFileEntry) { console.log(chrome.runtime.lastError); writableFileEntry.createWriter(function(writer) { writer.onerror = errorHandler; writer.onwriteend = function(e) { console.log('write complete'); }; writer.write(new Blob([arr], {type: 'text/plain'})); }, errorHandler); }); } initFs(); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload