File reader not working PhoneGap Windows - javascript

I'm developing an Windows app on Visual Studio 2012 with cordova. I tried to create and then after read the content of a .txt file.
This is what I tried,
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
// alert("File created");
fileSystem.root.getFile("textfile.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
// alert(fileEntry.fullPath);
fileEntry.createWriter(gotFileWriter, fail);
}
function gotFileWriter(writer) {
/*writer.onwrite = function(evt) {
fileRead();
alert("write success");
console.log("write success");
}; */
/* writer.onerror = function(evt) {
alert("Writing abort");
}; */
/* writer.onprogress = function(evt) {
alert("Progress writing file");
}; */
writer.onwritestart = function(parents) {
// alert("Starts" + parents);
}
writer.onwriteend = function(parent) {
alert("Success or Fail "+ parent.target);
};
writer.write("some sample text");
// alert("Writing Data" + writer);
// contents of file now 'some sample text'
writer.truncate(11);
// contents of file now 'some sample'
writer.seek(4);
// contents of file still 'some sample' but file pointer is after the 'e' in 'some'
writer.write(" different text");
// contents of file now 'some different text'
}
function fileRead(){
//alert("inside file read");
fileSystem.root.getFile('textfile.txt', {create: false, exclusive: false}, gotFileEntryForRead, fail);
}
function gotFileEntryForRead(fileEntry) {
alert(fileEntry);
//alert("inside got file entry for read");
fileEntry.file(gotFile, fail);
}
function gotFile(file){
//readDataUrl(file);
readAsText(file);
}
function readDataUrl(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
console.log("Read as data URL");
console.log(evt.target.result);
};
reader.readAsDataURL(file);
}
function readAsText(file) {
//alert("inside readAsText");
var reader = new FileReader();
reader.onloadend = function(evt) {
if(evt.target.result == null) {
alert("File doesn't exists");
} else {
alert("File exists");
}
alert(evt.target.result);
console.log("Read as text");
console.log(evt.target.result);
};
reader.readAsText(file);
}
function fail(evt) {
alert("Fail");
//alert("Error:" + evt.target.error.code);
//console.log(evt.target.error.code);
}
The file is created as the property onwrite is getting called but When I tried to see the content of file using alert alert(evt.target.result); then nothing happed.
Please help
Thanks

You are using fail function in gotFileEntry,onDeviceReady and in gotFS functions. These functions perform requestFileSystem,getFile,and writeFile functionality. How would you debug that where is the error. So, write their separate fail handler function to easily go the error and then fix the error. In this case you will not accurately got the error.
secondly check that weather you have add the permission in cordova file or not.
Hint: some time emulator do not offer full functionality as real devices do. Also test the app in a real device.

Related

Get a file base64 from the URI

i have been trying to get the base64 of a file. however i could not use the plugin for some reason.
window.resolveLocalFileSystemUrl(path, gotFile, fail);
This above code gives me a:
"Property 'resolveLocalFileSystemUrl' does not exist on type 'Window'."
error.
Is there any way to fix it? i have the plugin installed. Also, i have tried(from other stackoverflow answers)
window.resolveLocalFileSystemUri(path, gotFile, fail);
window.resolveLocalFileSystemURL(path, gotFile, fail);
window.resolveLocalFileSystemURI(path, gotFile, fail);
OR if there is another way to retrieve the base64, please assist.
By the way, im using filechoose to open and choose the file.
i have solved the issue by re-installing the plugin.
getFileContentAsBase64(path, callback){
window.resolveLocalFileSystemURL(path, gotFile, fail);
function fail(e){
alert('Cannot found requested file');
}
function gotFile(fileEntry){
fileEntry.file(function (file){
var reader = new FileReader();
reader.onloadend = function(e){
var content = this.result;
callback(content);
}
reader.readAsDataURL(file);
});
}
}
the above code allows you to convert a dataURL(file/image/pdf anything) into base64.
you can call it by:
getFileContentAsBase64(obj.toInternalURL().toString(), function (base64File) {
console.log(base64file);
}
Thanks everyone for the help!
I haven't found anywhere default function name window.resolveLocalFileSystemUri it's a resolveLocalFileSystemURL
Try Below
resolveLocalFileSystemURL(path, function(entry) {
var nativePath = entry.toURL();
console.log('Native URI: ' + nativePath);
document.getElementById('image').src = nativePath;
});
Source is here
Make sure you have installed file-transfer plugin.

Cordova File Plugin

I'm tearing my hair out trying to get the Cordova File Plugin and Cordova File Transfer to work, My code (below) should create a new folder 'Makes' and download an image into that new folder, but instead it just throws error code 1, but I can't figure out why, can someone please point me in the right direction:
function onAppReady(){
if(navigator.splashscreen && navigator.splashscreen.hide){
navigator.splashscreen.hide();
}
updateImages();
}
function updateImages(){
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail);
function fileSystemSuccess(fileSystem){
var directoryEntry=fileSystem.root;
directoryEntry.getDirectory("Makes", {create:true, exclusive:false}, onDirectorySuccess, onDirectoryFail);
}
function onDirectorySuccess(parent){
var rootdir = fileSystem.root;
var fp = parent.toURL();
var download_link=encodeURI('https://upload.wikimedia.org/wikipedia/en/thumb/8/80/Wikipedia-logo-v2.svg/1122px-Wikipedia-logo-v2.svg.png');
var fpFull=fp+'Makes/Wikipedia.png';
filetransfer(download_link, fpFull);
}
function onDirectoryFail(error){alert("Unable to create new directory: "+error.code);}
function fileSystemFail(evt){alert(evt.target.error.code);}
}
function filetransfer(download_link, fp){
var fileTransfer=new FileTransfer();
fileTransfer.download(download_link, fp, function(entry){
console.log("download complete: "+entry.fullPath);
},
function(error){
console.log("download error: "+error.code+' - '+error.source+' - '+error.target);
});
}
document.addEventListener("app.Ready", onAppReady, false);

Apache cordova - unable to write to file in android

I am trying to create a file in the PERSISTENT (sd card) storage using cordova's api in javascript. I am able to create the file, but unable to write data to it (data is empty on read). Below is my JS code:
$(document).ready(function() {
document.addEventListener("deviceready", onDeviceReady, false);
});
function onDeviceReady() {
writeTestList();
}
function writeTestList() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFileSystemForWrite, function() { showMsg('Unable to access file system for write'); });
}
function gotFileSystemForWrite(fileSystem) {
fileSystem.root.getFile('testList.txt', {create: true, exclusive: false}, gotFileEntryForWrite, function() { showMsg('Unable to access file for write'); });
}
function gotFileEntryForWrite(fileEntry) {
fileEntry.createWriter(gotFileWriter, function() { showMsg('Unable to create test list'); });
}
function gotFileWriter(writer) {
writer.onwrite = function(evt) {
// show a message
alert('created the test lists');
readSavedContent();
};
writer.onerror = function(evt) {
alert(JSON.stringify(evt));
};
writer.write("raj");
alert(writer.length);
}
The control always goes inside onerror of writer and on alerting the event obj, i see a type: "error", error: "JSON error". I completely don't get this. I am writing to a txt file, but getting json error. I have put write permissions in android manifest. Also, I have storage plugin for cordova enabled in config.xml preferences. People, pls help me out with this..!!
Unable to fix this issue in cordova 3.0. Moved to cordova 2.9 and everything works perfectly.

Get all filenames using Phonegap file API

I am using Phonegap file upload to upload SVG files to my server. It's working fine. But I need to take all SVG files from the iPad's absolute path and send them to my server. I don't know how to get all .svg files using Phonegap's file API, so that I can send to the server looping through file names. Please tell me how to do this.
My code for file upload is:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
fileSystem.root.getFile("image5_2.jpg.svg", {create: true, exclusive: false}, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
var localpath=fileEntry.fullPath;
uploadPhoto(localpath);
}
function uploadPhoto(imageURI) {
var options = new FileUploadOptions();
var ft = new FileTransfer();
ft.upload(imageURI, "http://192.168.1.54:8080/POC/fileUploader", win, fail, options);
}
You need to create a DirectoryReader from the root FileSystem and loop through all the entries looking for .svg files.
function gotFS(fileSystem) {
var reader = fileSystem.root.createReader();
reader.readEntries(gotList, fail);
}
function gotList(entries) {
var i;
for (i=0; i<entries.length; i++) {
if (entries[i].name.indexOf(".svg") != -1) {
uploadPhoto(entries[i].fullPath);
}
}
}
You may have to make some minor edits to this code but it should get you started.

How to read a file from an SD card in PhoneGap?

I am trying to follow the example that is provided on PhoneGap's documentation, except instead of calling the window.requestFileSystem... inside the onDeviceReady, I am calling that whenever I actually need to access a certain file. For some reason, my code does not seem to go past that line.
function pullSelectRecord(link)
{
selectedFile = link;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
alert("this is" + selectedFile);
fileSystem.root.getFile(link, null, gotFileEntry, fail);
}
function gotFileEntry(fileEntry) {
fileEntry.file(gotFile, fail);
}
function gotFile(file){
readDataUrl(file);
readAsText(file);
}
function readAsText(file) {
var reader = new FileReader();
reader.onloadend = function(evt) {
alert(evt.target.result);
};
reader.readAsText(file);
}
You can call requestFileSystem from anywhere. If you are not getting past that line you'll need to look in "adb logcat" to see what the error could be. Also, make sure your Manifest.xml has the write external storage permission.

Categories

Resources