hello , I'm using the method httpclient download a pdf from titanium cloud . The binary file is saved in applicationDataDirectory with the corresponding name of the cloud ... but when I download such a file 7MB I saw that are saved 14MB in applicationDataDirectory. The file is saved twice? would know someone explain why ? thanks in advance! This is my code:
Cloud.Files.show({ file_id: evt.id }, function (e) { if (e.success) { var file = e.files[0]; if (file.processed === true) { var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, file.name); if (!f.exists()) { var isDownloading = false; var percent_done = 0; var xhr = Titanium.Network.createHTTPClient({ onload: function() { Ti.API.info('isDownloading = ' + isDownloading); if (isDownloading === false) { dialog('download interrotto'); } else{ if (this.responseData !== null) { if (f.write(this.responseData) === false) { dialog('Salvataggio FILE non riusciuto, ritenta il download...'); isDownloading = false; f = null; } else{ Ti.API.info('PDF salvato in: ' + f.resolve()); ind.message = "Download complete"; if (isAndroid) { launch(copyToTemp(f, file.name)); } else{ var docViewer = Ti.UI.iOS.createDocumentViewer({url: f.resolve()}); docViewer.show(); } isDownloading = false; f = null; }; } else{ dialog('Download del FILE non riuscito, riprova più tardi...'); } } }, onerror: function () { dialog('onerror: FILE download non riusciuto, riprova più tardi...'); activityIndicator.hide(); f = null; }, ondatastream: function (arg) { activityIndicator.hide(); ind.show(); ind.value = arg.progress; var curr_percent_done = parseInt(arg.progress*100); if (percent_done == curr_percent_done) return; percent_done = curr_percent_done; console.log(percent_done+"% done. readyState: "+this.readyState); }, onreadystatechange: function () { Ti.API.info('READYSTATE: ' + this.readyState); if (this.readyState <= 3) { isDownloading = true; Ti.API.info('isDownloading = ' + isDownloading); } }, timeout: 60000 }); xhr.open('GET', file.url); xhr.send(); win.addEventListener('close', function() { if (isAndroid) { var tempDir = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory, 'tempDir'); if(tempDir.exists()) { tempDir.deleteDirectory(true); } }; if (isDownloading === true) { isDownloading = false; xhr.abort(); f = null; } }); }and these are the functions for the launch of intent and creating a temporary file for Android:
function copyToTemp (srcFile, fileName) { var tempDir = Ti.Filesystem.getFile(Ti.Filesystem.tempDirectory, 'tempDir'); if(!tempDir.exists()) { tempDir.createDirectory(); } var tempFile = Ti.Filesystem.getFile(tempDir.resolve(), fileName); tempFile.write(srcFile.read()); return tempFile; } // launch intent to read pdf function launch (file) { Ti.API.info(" ---------> launching pdf path: " + file.getNativePath()); var intent = Ti.Android.createIntent({ action: Ti.Android.ACTION_VIEW, data: file.getNativePath(), type: "application/pdf" }); Ti.Android.currentActivity.startActivity(intent); }