Friday, March 9, 2018

How to choose a pdf/doc/image file from the device and upload to the server.

You can try with the following sample code


var window = Ti.UI.createWindow({
backgroundColor : 'red',
layout : "vertical"
});
// Create a Button.
var request = Ti.UI.createButton({
title : 'GO',
height : 100,
width : 100,
backgroundImage : "motiur.jpg",
top : 50
});
request.addEventListener('click', function(e) {
var intent = Ti.Android.createIntent({
action : Ti.Android.ACTION_GET_CONTENT,
type : "application/pdf"

});
intent.addCategory(Ti.Android.CATEGORY_OPENABLE);
var x = Ti.Android.createIntentChooser(intent, "Select");
window.getActivity().startActivityForResult(x, function(e) {

//alert("fileData:" + JSON.parse(e.intent.data));
var doc = Ti.Filesystem.getFile(e.intent.data);
Ti.API.info(JSON.stringify(doc));

var file = Ti.Filesystem.getFile(doc.nativePath);
Ti.API.info("File:" + doc.nativePath);
alert("File path:" + doc.nativePath);

// write here http code for file uploading to the server.

});
});
window.add(request);
window.open();

Thanks!