Skip to content

Instantly share code, notes, and snippets.

@timoa
Created September 16, 2012 18:01
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save timoa/3733505 to your computer and use it in GitHub Desktop.
Save timoa/3733505 to your computer and use it in GitHub Desktop.
Save file iOS and Android with Appcelerator Titanium Mobile SDK (http://timoa.com/en/2012/07/appcelerator-nativepath-on-ios-and-android/)
// OS
if(Ti.Platform.osname === 'iPhone' || Ti.Platform.osname === 'iPad') {
var ios = true;
}
/**
* Save file function
*
* @file: Binary file (Blob)
* @filename: Name of file (String)
*
*/
function saveFile(_args) {
// Test if External Storage (Android only)
if(Ti.Filesystem.isExternalStoragePresent()){
var file = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory, _args.filename);
}
// No SD or iOS
else {
var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, _args.filename);
}
// Save file
file.write(_args.file);
// Debug: Test if file exist now
if(file.exists) {
Ti.API.info('[saveFile] Saved: YES! (' + file.nativePath + ')');
} else {
Ti.API.info('[saveFile] Saved: NO!');
}
// Return full path of file
if(ios && Ti.version <= '1.8.2') {
var iosPath = Ti.Filesystem.applicationDataDirectory + _args.filename;
iosPath = iosPath.replace('file://','app://');
return iosPath;
}
else {
return file.nativePath;
}
};
// To use, simply call the function in this way :
var pathToImage = saveFile({
file: ***blob***,
filename: 'image.png'
});
@AhmedTheGeek
Copy link

Thanks a lot for posting this :)

@chmiiller
Copy link

Thanks for this code snippet! It's really helpful =)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment