1267

Failed to write 10 lines to a file using loop in Javascript (PhoneGap)

Question:

I am trying to write atleast 10 lines to a file using loops to append multiple lines in a file using PhoneGap, but everytime I run this code below, only able to add 1 line to a file as a newline. But 10 lines are not being printed in 10 iteration. Please suggest me how to rectify my problem using phoneGap javascript.

function gotFileWriter(writer) { for (var z = 0; z<= 10; z++) { console.log("normal write success"); writer.onwrite = function(evt) { console.log("write success"); }; writer.onwriteend = function(evt) { console.log("write end"); } writer.seek(writer.length); writer.write("id,updated,created,fname,lname,company,tags,type,label,value,source,sourceid\r\n"); writer.abort(); // I tried even commenting } }

Answer1:

Have you try this ?

function gotFileWriter(writer) { console.log("normal write success"); writer.onwrite = function(evt) { console.log("write success"); }; writer.onwriteend = function(evt) { console.log("write end"); } for (var z = 0; z< 10; z++) { writer.seek(writer.length); writer.write("id,updated,created,fname,lname,company,tags,type,label,value,source,sourceid\r\n"); writer.abort(); // I tried even commenting } }

Answer2:

This did the trick for me

Build one big string:

function gotFileWriter(writer) { var bigString = ''; for (var i=0; i<numberOfLines; i++) { bigString += line[i]; bigString += '\r\n'; } ...

Then do a massive write to the file:

... writer.onwrite = function (evt) { ... }; writer.onwriteend = function (evt) { ... }; writer.seek(writer.length); writer.write (bigString); }

Answer3:

You can use this for appending the text to file instead of looping

function gotFileWriter(writer) { writer.seek(writer.length); writer.write(" - append- \n"); }

Recommend

  • How to rotate object around local axis in OpenGL?
  • How to insert javascript into mysql database?
  • Using EventHandler in C++/CLI
  • TypeError: firebase.messaging is not a function in node.js
  • Notification.alert is not working in phonegap build 3.1.0
  • Is it really posible to close a PhoneGap App?
  • Phonegap Android App, open links within app?
  • Iterate twice through a DataReader
  • Accessing the variables from a PHP Anonymous Function
  • how to populate a SQLite database and use that database in phonegap?
  • Redshift Querying: error xx000 disk full redshift
  • calculate gradient output for Theta update rule
  • MongoDb aggregation
  • Rails Find when some params will be blank
  • Meteor: Do Something On Email Verification Confirmation
  • Q promise. Difference between .when and .then
  • Update CALayer sublayers immediately
  • Deselecting radio buttons while keeping the View Model in synch
  • HTML download movie download link
  • Volley JsonObjectRequest send headers in GET Request
  • Is there a amazon webstore API for customers?
  • How to get a value (ex: baseURL) in every Karate feature?
  • Updating server-side rendering client-side
  • Join two tables and save into third-sql
  • Perl system calls when running as another user using sudo
  • How to model a transition system with SPIN
  • Can a Chrome extension content script make an jQuery AJAX request for an html file that is itself a
  • Upload files with Ajax and Jquery
  • ORA-29908: missing primary invocation for ancillary operator
  • How can I estimate amount of memory left with calling System.gc()?
  • How to pass list parameters for each object using Spring MVC?
  • AngularJs get employee from factory
  • Proper way to use connect-multiparty with express.js?
  • How to set the response of a form post action to a iframe source?
  • Setting background image for body element in xhtml (for different monitors and resolutions)
  • Binding checkboxes to object values in AngularJs
  • Converting MP3 duration time
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?