Skip to content

Instantly share code, notes, and snippets.

@MotiurRahman
Created December 24, 2014 04:50
Show Gist options
  • Save MotiurRahman/26bf43bb239533aa73e1 to your computer and use it in GitHub Desktop.
Save MotiurRahman/26bf43bb239533aa73e1 to your computer and use it in GitHub Desktop.
Download pdf file and show webView.
var win = Titanium.UI.createWindow({
title : "Download",
layout : 'vertical'
});
ind = Titanium.UI.createProgressBar({
width : 200,
height : 50,
min : 0,
max : 1,
value : 0,
top : 10,
message : 'Downloading File',
font : {
fontSize : 12,
fontWeight : 'bold'
},
color : '#888'
});
Ti.Platform.name === 'iPhone' && (ind.style = Titanium.UI.iPhone.ProgressBarStyle.PLAIN);
win.add(ind);
ind.show();
var b1 = Titanium.UI.createButton({
title : 'Set Web View (url)',
height : 40,
width : 200,
top : 20
});
win.add(b1);
b1.addEventListener('click', function() {
ind.value = 0;
var filename = 'test.pdf';
c = Titanium.Network.createHTTPClient();
c.setTimeout(10000);
c.onload = function() {
Ti.API.info('IN ONLOAD ');
ind.value = 1.0;
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename);
f.write(this.responseData);
Ti.API.info("path:" + f.nativePath);
var wv = Ti.UI.createWebView({
url : f.nativePath,
bottom : 0,
left : 0,
right : 0,
top : 20
});
win.add(wv);
};
c.ondatastream = function(e) {
ind.value = e.progress;
Ti.API.info('ONDATASTREAM1 - PROGRESS: ' + e.progress);
};
c.onerror = function(e) {
Ti.API.info('XHR Error ' + e.error);
};
c.open('GET', 'http://www.appcelerator.com/assets/The_iPad_App_Wave.pdf');
// c.file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, filename);
// send the data
c.send();
});
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment