Cordova Move File using the file url - javascript

How can I move a file using the URL I get from the Camera?
neither successCallback nor errorCallback is called by the function moveTo. Can anyone tell me what I am doing wrong and what a possible solution looks like?
function successCallback(entry) {
console.log("New Path: " + entry.fullPath);
alert("Success. New Path: " + entry.fullPath);
}
function errorCallback(error) {
console.log("Error:" + error.code)
alert(error.code);
}
// fileUri = file:///emu/0/android/cache/something.jpg
function moveFile(fileUri) {
newFileUri = cordova.file.dataDirectory + "images/";
oldFileUri = fileUri;
fileExt = "." + oldFileUri.split('.').pop();
newFileName = guid("car") + fileExt;
// move the file to a new directory and rename it
fileUri.moveTo(cordova.file.dataDirectory, newFileName, successCallback, errorCallback);
}
I am using Cordova version 4.1.2 Also installed the Cordova File Plugin

You're trying to call the function moveTo on a String.
moveTO is not a function of String but of fileEntry. So first thing you need to do is get a fileEntry from your URI.
For that you'll call window.resolveLocalFileSystemURL :
function moveFile(fileUri) {
window.resolveLocalFileSystemURL(
fileUri,
function(fileEntry){
newFileUri = cordova.file.dataDirectory + "images/";
oldFileUri = fileUri;
fileExt = "." + oldFileUri.split('.').pop();
newFileName = guid("car") + fileExt;
window.resolveLocalFileSystemURL(newFileUri,
function(dirEntry) {
// move the file to a new directory and rename it
fileEntry.moveTo(dirEntry, newFileName, successCallback, errorCallback);
},
errorCallback);
},
errorCallback);
}

Related

How to read object written through cordova file plugin?

I read on Cordova's documentation for android platform a code snipped and tried to use it for writing a JS object on a text file. The object gets successfully written but when I read it with FileReader API I can't get output as expected.
function writeFile(fileEntry, dataObj, isAppend) {
// Create a FileWriter object for our FileEntry (log.txt).
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function() {
console.log("Successful file read...");
readFile(fileEntry);
};
fileWriter.onerror = function (e) {
console.log("Failed file read: " + e.toString());
};
// If we are appending data to file, go to the end of the file.
if (isAppend) {
try {
fileWriter.seek(fileWriter.length);
}
catch (e) {
console.log("file doesn't exist!");
}
}
fileWriter.write(dataObj);
});
}
function readFile(fileEntry) {
fileEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function() {
console.log("Successful file read: " + this.result);
//displayFileData(fileEntry.fullPath + ": " + this.result);
};
reader.onload = function(){
k=reader.readAsText(file);
};
reader.readAsText(file);
},onErrorLoadFs );
}
Format of object I want to read :
function sub(name,absent,present){
this.name=name;
this.absent=absent;
this.present=present;
}
var S = new sub('Physics',1,3);
var k= new sub();
What exactly I want to do :
I am writing an object S on the file which appears like this when opened
{"name":"Physics","absent":1, "present" : 3}
Now after reading the file (which in my case is filetoAppend.txt) I want to assign these values to another object k so that when I run k.name, Physics is shown as output.
console output
k
"{"name":"Physics","absent":1,"present":3}"
k.name
undefined
With the Cordova File Plugin, there are two essential pieces of information to remember:
1.Like all Cordova plugins, you have to wait for the deviceready event before you try anything,
2.Then, Use window.resolveLocalFileSystemURL(<path>, <successHandler>, <errorHandler>)
window.resolveLocalFileSystemURL() returns a FileEntry or DirectoryEntry instance (depending on whether you gave a file or a directory as path as its first parameter), which you can then work with.
WRITING TO A FILE
document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady() {
function writeToFile(fileName, data) {
data = JSON.stringify(data, null, '\t');
window.resolveLocalFileSystemURL(cordova.file.dataDirectory, function (directoryEntry) {
directoryEntry.getFile(fileName, { create: true }, function (fileEntry) {
fileEntry.createWriter(function (fileWriter) {
fileWriter.onwriteend = function (e) {
// for real-world usage, you might consider passing a success callback
console.log('Write of file "' + fileName + '"" completed.');
};
fileWriter.onerror = function (e) {
// you could hook this up with our global error handler, or pass in an error callback
console.log('Write failed: ' + e.toString());
};
var blob = new Blob([data], { type: 'text/plain' });
fileWriter.write(blob);
}, errorHandler.bind(null, fileName));
}, errorHandler.bind(null, fileName));
}, errorHandler.bind(null, fileName));
}
writeToFile('example.json', { foo: 'bar' });
}
WRITING FROM FILE
document.addEventListener('deviceready', onDeviceReady, false);
function onDeviceReady() {
function readFromFile(fileName, cb) {
var pathToFile = cordova.file.dataDirectory + fileName;
window.resolveLocalFileSystemURL(pathToFile, function (fileEntry) {
fileEntry.file(function (file) {
var reader = new FileReader();
reader.onloadend = function (e) {
cb(JSON.parse(this.result));
};
reader.readAsText(file);
}, errorHandler.bind(null, fileName));
}, errorHandler.bind(null, fileName));
}
var fileData;
readFromFile('data.json', function (data) {
fileData = data;
});
}
cb is the callback function that you need to pass when calling this function
For full reference use:https://www.neontribe.co.uk/cordova-file-plugin-examples/
Updated based on your updated Question
In reader.onloadend you can get the result of the file and assign to your output object k or can call callback function incase.
reader.onloadend = function (e) {
//cb(JSON.parse(this.result));
var k=JSON.parse(this.result);
console.log(k.name + ", " + k.absent+ ", " + k.present);
};
var k = JSON.parse('{"name":"Physics","absent":1, "present" : 3}');
console.log(k.name + ", " + k.absent + ", " + k.present);

AWS S3 SDK: how do I get the filename from the progress callback in a multi file upload?

So I'm using the AWS S3 Javascript SDK to upload multiple files. This code below works well, but I do have one issue. The function (evt) for progress is called asynchronously and I cannot get which filename it was called for. evt does not include filename. Is there a way to know?
How do I output the filename to console (where I show the >>> I NEED THE FILENAME HERE <<<)
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (files[i]) {
var params = {Key: file.name, ContentType: file.type, Body: file};
bucket.upload(params).on('httpUploadProgress', function(evt) {
console.log("Uploaded " + >>> I NEED THE FILENAME HERE <<< + " " + parseInt((evt.loaded * 100) / evt.total)+'%');
}).send(function(err, data) {
//alert("File uploaded successfully -- " + err);
});
}
}
evt.key -> should give the file name that is being uploaded...!!!
Edited:
here is the full code I am using for the managed upload
app.post('/uploadLargeFile',upload.array('file', 20),function(req,res){
console.log("received File")
var file = req.files;
for(var i=0;i<req.files.length;i++){
var file = req.files[i];
uploadLargeFiles(file);
}
})
function uploadLargeFiles(file){
var params={
ACL :'public-read',
Body : new Buffer(file.buffer),
Bucket:'ascendon1',
ContentType:file.mimetype,
Key:file.originalname
}
var managedUpload = new AWS.S3.ManagedUpload({
params:params
});
managedUpload.on('httpUploadProgress', function(progress) {
console.log(progress);
});
managedUpload.send(function(err, data) {
console.log(err, data);
});
}

Node File transfer uploads x bytes of image in images directory but is corrupt

I am using Busboy module of Node for parsing files. First upload a file -> Push that uploaded file to images directory. I dont know why but the code is transferring bytes i.e. it does create a image with proper bytes but when clicked on file, its corrupt. Here is my code:
var busboy = new Busboy({ headers: req.headers });
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
console.log('File [' + fieldname + ']: filename: ' + filename + ', encoding: ' + encoding + ', mimetype: ' + mimetype);
file.on('data', function(data) {
var fstream = fs.createWriteStream('./images/' + filename);
file.pipe(fstream);
fstream.on('close', function () {
console.log("Upload Finished of " + filename);
});
console.log('File [' + fieldname + '] got ' + data.length + ' bytes');
});
file.on('end', function() {
console.log('File [' + fieldname + '] Finished');
});
});
data events can be emitted multiple times. The solution here is simple: just pipe file to a writable stream once. For example:
var crypto = require('crypto');
var path = require('path');
// ...
var busboy = new Busboy({ headers: req.headers });
busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
// You will want to somehow sanitize `filename` if you are going to use
// it when use it as part of the filename on disk, as it could be maliciously
// constructed to overwrite to other parts of your filesystem.
//
// The solution I use here is to simply hash the filename, but you could
// use `path.resolve('./images/', filename)` instead and check that the
// result starts with `__dirname + '/images/'`.
var ext = path.extname(filename);
filename = crypto.createHash('sha1')
.update(filename, 'utf8')
.digest('hex') + ext;
var diskStream = fs.createWriteStream('./images/' + filename);
file.pipe(diskStream).on('finish', function() {
console.log('Finished writing file');
});
});

HTML5 Read File Returns Results before File is Read

I am trying to read a text file and return the results.
So far I am able to read the file, but when I try to return the results the results are returned before the file read is done.
How do I force the function to wait for the results?
I figure its something to do with a callback but don't know how to implement it.
MyFile.prototype.readFile = function(fileDir, fileName) {
var strText = "";
window.resolveLocalFileSystemURL(fileDir, gotDir, fail);
function gotDir(dir) {
dir.getFile(fileName, {create: true}, gotFile, fail);
}
function gotFile(fileEntry) {
fileEntry.file(gotRead, fail);
}
function gotRead(file) {
var reader = new FileReader();
reader.onerror = function(evt) {
console.log("Reading " + file.name + " Failed");
};
reader.onloadstart = function(evt) {
console.log("Read " + file.name + " Starting");
};
reader.onload = function(evt) {
console.log("Read " + file.name + " Successful");
};
reader.onloadend = function(evt) {
console.log("Read " + file.name + " Ending");
endRead(evt.target.result);
};
reader.onprogress = function(evt) {
console.log(evt.loaded + " of " + evt.total);
};
reader.readAsText(file);
}
function endRead(value) {
strText = value;
}
//returns null value before file read finishes
return strText;
};
The FileSystem api is asynchronous, meaning you have to pass a callback to your readFile method which will get called when the file is read. It's not possible to synchronously do it, see this.
You can change your signature to this:
MyFile.prototype.readFile = function(fileDir, fileName, endRead) {
And remove the following lines
function endRead(value) {
strText = value;
}
//returns null value before file read finishes
return strText;
Now, you can pass a function as the third argument to readFile method and access the file contents using the first parameter of that function.

How to create a directory and file in that directory using phonegap file api?

I am using phonegap file api to create a directory and create a file in the directory created. The directory is getting created, but the file is not getting created in the directory.
The code I am using is:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
var dataDir = fileSystem.root.getDirectory("data", {create: true});
var file = dataDir.getFile("lockfile.txt", {create: true, exclusive: true});
}
The directory data is created but lockfile.txt is not getting created.
You need to call the code in an async manner:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
fileSystem.root.getDirectory("data", {create: true}, gotDir);
}
function gotDir(dirEntry) {
dirEntry.getFile("lockfile.txt", {create: true, exclusive: true}, gotFile);
}
function gotFile(fileEntry) {
// Do something with fileEntry here
}
Is this work?
var file = fileSystem.root.getFile("data" + "lockfile.txt", {create: true, exclusive: true});
Download file from the url to your device using phonegap
It is working 3.0 and up to for iOS and android
var folderName = 'xyz';
var fileName;
function downloadFile(URL) {
//step to request a file system
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fileSystemSuccess, fileSystemFail);
function fileSystemSuccess(fileSystem) {
var download_link = encodeURI(URL);
fileName = download_link.substr(download_link.lastIndexOf('/') + 1); //Get filename of URL
var directoryEntry = fileSystem.root; // to get root path of directory
directoryEntry.getDirectory(folderName, {
create: true,
exclusive: false
}, onDirectorySuccess, onDirectoryFail); // creating folder in sdcard
var rootdir = fileSystem.root;
var fp = fileSystem.root.toNativeURL(); // Returns Fullpath of local directory
fp = fp + "/" + folderName + "/" + fileName; // fullpath and name of the file which we want to give
// download function call
filetransfer(download_link, fp);
}
function onDirectorySuccess(parent) {
// Directory created successfuly
}
function onDirectoryFail(error) {
//Error while creating directory
alert("Unable to create new directory: " + error.code);
}
function fileSystemFail(evt) {
//Unable to access file system
alert(evt.target.error.code);
}
}
function filetransfer(download_link, fp) {
var fileTransfer = new FileTransfer();
// File download function with URL and local path
fileTransfer.download(download_link, fp,
function(entry) {
alert("download complete: " + entry.fullPath);
},
function(error) {
//Download abort errors or download failed errors
alert("download error source " + error.source);
}
);
}
function download(URL, fileName){
var folderName = 'xyz';
var uri = encodeURI(URL);
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
function (fileSystem) {
var directoryEntry = fileSystem.root; // to get root path of directory
directoryEntry.getDirectory(folderName, {
create: true,
exclusive: false
}, onDirectorySuccess, onDirectoryFail);
var filename = fileSystem.root.toURL() + folderName + "/" + uri.substr(uri.lastIndexOf("/") + 1);
var fileTransfer = new FileTransfer();
fileTransfer.download(uri, filename,
function(entry) { // download success
var path = entry.toURL(); //**THIS IS WHAT I NEED**
window.plugins.toast.showLongBottom("Download Completed: " + entry.fullPath, function (a) {
}, function (b) {
});
},
function(error) {
console.log("error")
} // irrelevant download error
);`enter code here`
},
function(error) {
console.log("error2")
} // irrelevant request fileSystem error
);
function onDirectorySuccess(parent) {
// Directory created successfuly
console.log("Directory created successfuly: " + JSON.stringify(parent));
var fp = (parent.nativeURL) + fileName;
filetransfer(download_link, fp);
}
function onDirectoryFail(error) {
//Error while creating directory
alert("Unable to create new directory: " + error.code);
}
}

Categories

Resources