Thursday, 30 June 2011

Upload a File to a Remote Server with Phonegap

I recently started looking at PhoneGap as a way to build native smartphone applications without having to learn another programming language. The process, so far, has been going very well. I’ve been able to build a basic application with very little trouble. I did, however, hit a big hiccup in development when I decided to try and upload a file from a phone to a remote location.

The unfortunate part of this hiccup was that I think its primary cause is rooted in poor documentation.In this post, I want to explain how I was able to upload a photo from a phone, using PhoneGap (and PHP on the remote server), to a remote server. I am going to assume that you have some knowledge of PhoneGap and already have a method, either the Android SDK or the iPhone SDK, to test your native application.

The HTML / JavascriptRather then use a jQuery Ajax call, I am relying on PhoneGap’s FileTransfer object to post the image to a remote server. The code below is all you need to add to your “index.html” PhoneGap file for a basic upload.

Make sure, if you use the example code below, to add your server’s URL in the appropriately marked place.As you can see, the code above allows you to browse for a photo in the device’s photo library using the function getPhoto(). Once a photo is chosen, it gets POSTed as multi-part data to the server.

You will notice a couple of things. 1. I commented out the options.fileName variable. The default name for an uploaded image is “image.jpg.” Also, I’ve included some test parameters in the upload data which will be POSTed to your server as well, but these are optional.

The PHPNow that you are able to find a file and POST it to a server, you’ll need a server side script to handle the data. Here is a very basic PHP example on handling the multi-part form data (image) that was just sent from the device.

The above code uses PHP’s move_uploaded_file() to move the uploaded file from a temporary location to a new directory. To make sure it works, change “/srv/www/upload/” to a directory on your server. I usually have to pass an absolute file-path as the second variable in the move_uploaded_file() for it to work.

You can learn more about PHP uploads in a previous post of mine.FinishedAbove is a short example of how to upload a file to a remote server using PhoneGap, including how to handle that data once it gets to the server.

PHP Send Email Using Authenticated SMTP Mail Server In Real Time

PHP has mail() function to send an email to users.
However this mail() will not work:=>
If sendmail (or compatible binary) is not installed=>
If Apache Web server / Lighttpd running in chrooted jail=>
And your smtp server needs an authentication before sending an email=>

Or you just need to send email using PHP PEARIn all these cases you need to use PHP PEAR's Mail:: interface.

It defines the interface for implementing mailers under the PEAR hierarchy, and provides supporting functions which are useful in multiple mailer backends. In this tip you will learn about how to send an e-mail directly to client smtp server in real time.PHP Pear's Mail.php is located in /usr/share/pear/ directory. Following is sample code to send an email via authenticated smtp server.

PHP send email using PHP SMTP mail Pear functions - Sample source codeFollowing code is well commented, you need to make necessary changes as per your
setup.send($recipients, $headers, $mailmsg);
?>

Sending smtp email from chrooted Apache or Lighttpd webserverRead following section, if you are running a secure chrooted Apache or Lighttpd web server. I have already written about setting php mail() function in chrooted jail.

If you are using chrooted jail server setup, copy all files from /usr/share/pear directory to /chroot-directory/usr/share/pear directory. For example if lighttpd chrooted jail located in /webroot directory, you need to type following commands to install PHP pear support:# mkdir -p /webroot/usr/share/pear# cd /webroot/usr/share/pear# cp -avr /usr/share/pear .

If PHP SAFE MODE is on, you must set /webroot/usr/share/pear directory permission to webserver username to allow access. Otherwise you will see error as follows:1-Nov-2006 09:43:19] PHP Warning: main(): SAFE MODE Restriction in effect. The script whose uid is 506 is not allowed to access /usr/share/pear/PEAR.php owned by uid 0 in /usr/share/pear/Mail.php on line 636.

So if webserver username is lighttpd or apache use following command to setup correct ownership:# chown lighttpd:lighttpd /webroot/usr/share/pear -ROR# chown apache:apache /webroot/usr/share/pear -RYou may also find modified wordpress WP-ContactForm plugin useful. It is a drop in form for users to contact you. It can be implemented on a page or a post.

Original authored by Ryan Duff, which use php mail() function to send email. I have modified the same to send email via my ISP authenticated gateway using PHP PEAR's Mail:: interface :D

Re: [PhoneGap] how to add get applicationDidBecomeActive and didFinishLaunchingWithOptions methods in Phonegap

Use the latest 0.9.6.applicationdidFinishLaunchingWithOptions is there,applicationDidBecomeActive is there but in the subclass PhoneGapDelegate.

For active/inactive events, use the "pause" and "resume" events in javascript instead to handle that event:http://docs.phonegap.com/phonegap_events_events.md.html

On 2011-06-29, at 3:00 AM, Abhi wrote:>
Hi,>>
Native apps on IOS have the following methods>
- (BOOL)application:(UIApplication *)application> didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions>
- (void)applicationDidBecomeActive:(UIApplication *)application>
> but Phonegap.0.9.4 has>
- (void)applicationDidFinishLaunching:(UIApplication *)application>
- (void)webViewDidFinishLoad:(UIWebView *)theWebView //this does not> trigger when app comes to foreground from background>>>>> Pls advice how to add the two triggers in Phonegap platform (required> to implement push notification all scenarios)>
- (BOOL)application:(UIApplication *)application>
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions>
- (void)applicationDidBecomeActive:(UIApplication *)application>> -->

You received this message because you are subscribed to the Google>
Groups "phonegap" group.> To post to this group, send email to phonegap@googlegroups.com>
To unsubscribe from this group, send email to>
phonegap+unsubscribe@googlegroups.com>

For more options, visit this group at>
http://groups.google.com/group/phonegap?hl=en?hl=en> >

For more info on PhoneGap or to download the code go to www.phonegap.com-- You received this message because you are subscribed to the GoogleGroups "phonegap" group.To post to this group, send email to phonegap@googlegroups.com

To unsubscribe from this group, send email tophonegap+unsubscribe@googlegroups.comFor more options, visit this group athttp://groups.google.com/group/phonegap?hl=en?hl=enFor more info on PhoneGap or to download the code go to www.phonegap.com

[nodejs] Joey Guerra wants to chat

-----------------------------------------------------------------------Joey Guerra wants to stay in better touch using some of Google's coolest newproducts.If you already have Gmail or Google Talk, visit:http://mail.google.com/mail/b-5c0a4ac672-e0278c1b8b-i0wmic9xVGZ7nAXpPY-Z5P-OqsUYou'll need to click this link to be able to chat with Joey Guerra.To get Gmail - a free email account from Google with over 2,800 megabytes ofstorage - and chat with Joey Guerra, visit:http://mail.google.com/mail/a-5c0a4ac672-e0278c1b8b-i0wmic9xVGZ7nAXpPY-Z5P-OqsUGmail offers:- Instant messaging right inside Gmail- Powerful spam protection- Built-in search for finding your messages and a helpful way of organizingemails into "conversations"- No pop-up ads or untargeted banners - just text ads and related informationthat are relevant to the content of your messagesAll this, and its yours for free. But wait, there's more! By opening a Gmailaccount, you also get access to Google Talk, Google's instant messagingservice:http://www.google.com/talk/Google Talk offers:- Web-based chat that you can use anywhere, without a download- A contact list that's synchronized with your Gmail account- Free, high quality PC-to-PC voice calls when you download the Google TalkclientWe're working hard to add new features and make improvements, so we might alsoask for your comments and suggestions periodically. We appreciate your help inmaking our products even better!Thanks,The Google TeamTo learn more about Gmail and Google Talk, visit:http://mail.google.com/mail/help/about.htmlhttp://www.google.com/talk/about.html(If clicking the URLs in this message does not work, copy and paste them intothe address bar of your browser).-- Job Board: http://jobs.nodejs.org/Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-GuidelinesYou received this message because you are subscribed to the GoogleGroups "nodejs" group.To post to this group, send email to nodejs@googlegroups.comTo unsubscribe from this group, send email tonodejs+unsubscribe@googlegroups.comFor more options, visit this group athttp://groups.google.com/group/nodejs?hl=en?hl=en

[PhoneGap] Re: iOS UIWebview - make it faster for PhoneGap!

+1 more to the pile :DOn Jun 29, 6:50 am, Shazron Abdullah wrote:>
Every-time there's a duplicate issue in Apple's Bug Reporter (kinda like voting for it), the more likely it will be implemented next time (we hope). Mine was duped. So please file an issue, it will only take 5 mins :)>>

File an issue athttp://bugreporter.apple.comand login with your Apple ID. Click on "New Problem">> Title: >

UIWebview: should support Nitro Javascript>> Product:> iPhone SDK>> Classification:> Feature (New)>> Description:> Enable Nitro Javascript for the UIWebview component, like Mobile Safari and home-screen web-apps.--

You received this message because you are subscribed to the GoogleGroups "phonegap" group.To post to this group, send email to phonegap@googlegroups.com

To unsubscribe from this group, send email tophonegap+unsubscribe@googlegroups.comFor more options, visit this group athttp://groups.google.com/group/phonegap?hl=en?hl=enFor more info on PhoneGap or to download the code go to www.phonegap.com

Re: [nodejs] Joey Guerra wants to chat

WAT?!On Wed, Jun 29, 2011 at 9:49 PM, Joey Guerra wrote: ----------------------------------------------------------------------- Joey Guerra wants to stay in better touch using some of Google's coolest new products. If you already have Gmail or Google Talk, visit: http://mail.google.com/mail/b-5c0a4ac672-e0278c1b8b-i0wmic9xVGZ7nAXpPY-Z5P-OqsU You'll need to click this link to be able to chat with Joey Guerra. To get Gmail - a free email account from Google with over 2,800 megabytes of storage - and chat with Joey Guerra, visit: http://mail.google.com/mail/a-5c0a4ac672-e0278c1b8b-i0wmic9xVGZ7nAXpPY-Z5P-OqsU Gmail offers: - Instant messaging right inside Gmail - Powerful spam protection - Built-in search for finding your messages and a helpful way of organizing emails into "conversations" - No pop-up ads or untargeted banners - just text ads and related information that are relevant to the content of your messages All this, and its yours for free. But wait, there's more! By opening a Gmail account, you also get access to Google Talk, Google's instant messaging service: http://www.google.com/talk/ Google Talk offers: - Web-based chat that you can use anywhere, without a download - A contact list that's synchronized with your Gmail account - Free, high quality PC-to-PC voice calls when you download the Google Talk client We're working hard to add new features and make improvements, so we might also ask for your comments and suggestions periodically. We appreciate your help in making our products even better! Thanks, The Google Team To learn more about Gmail and Google Talk, visit: http://mail.google.com/mail/help/about.html http://www.google.com/talk/about.html (If clicking the URLs in this message does not work, copy and paste them into the address bar of your browser). -- Job Board: http://jobs.nodejs.org/ Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines You received this message because you are subscribed to the Google Groups "nodejs" group. To post to this group, send email to nodejs@googlegroups.com To unsubscribe from this group, send email to nodejs+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/nodejs?hl=en?hl=en-- Job Board: http://jobs.nodejs.org/Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-GuidelinesYou received this message because you are subscribed to the GoogleGroups "nodejs" group.To post to this group, send email to nodejs@googlegroups.comTo unsubscribe from this group, send email tonodejs+unsubscribe@googlegroups.comFor more options, visit this group athttp://groups.google.com/group/nodejs?hl=en?hl=en

[PhoneGap] Re: Calling plugin method synchronously

Here is my correspondence with ShazronYou can use static libraries only, no third-party dynamic librariesare allowed. Apple's dynamic libraries are allowed of course (thingslike UIKit, etc that are part of the SDK) but not your own.- Hide quoted text -On Tue, Jun 28, 2011 at 5:01 AM, Sumedh wrote:> Hi Shazron,>> I wanted to know whether i can use shared libraries with phonegap plugins.>> What about Dynamic link Libraries, are they allowed as part of an iphone> app. I am not getting appropriate information about this.>> thanks.>> On Mon, Jun 27, 2011 at 6:03 PM, Sumedh wrote:>>>> Thanks sir.>>>>>> On Mon, Jun 27, 2011 at 5:31 PM, Shazron Abdullah>> wrote:>>>>>> Also i see PhoneGap uses UIWebvVew, so there are limits on scripts>>> executions, like 10 sec and 10 MB, can we set this values?>>>>>> No.>>>>>> Can i build my custom webkit for ios, so that i can have access to>>> jscore. I have done it on windows, and linux... I hope i can do same for MAC>>> OSX, but don't have any idea about IOS. Can you throw some light on this?>>>>>> You could. Look at the Couchbase (CouchDB) for iOS project, might be some>>> info there.********************************No, everything is async. It's a limitation on how PhoneGap works (atleast on iOS). For Android, it's possible but I'm not familiar withit.--Shazron AbdullahOn Monday, June 27, 2011 at 3:30 AM, Sumedh wrote:> Hi Shazron,>> I was going through plugins, all available plugin examples utility functions seems to be called in a thread and then return back the result in callback. Can i have a plugin which will return back the result to the caller, for which i hope call to plugin utility functions should be in the same thread.>> Hoping for a reply. Thanks.-- You received this message because you are subscribed to the GoogleGroups "phonegap" group.To post to this group, send email to phonegap@googlegroups.comTo unsubscribe from this group, send email tophonegap+unsubscribe@googlegroups.comFor more options, visit this group athttp://groups.google.com/group/phonegap?hl=en?hl=enFor more info on PhoneGap or to download the code go to www.phonegap.com

Re: [PhoneGap] Coverage Into using Phonegap.

Hey Drew,You are right. I didn't execute "document.addEventListener("deviceready", onDeviceReady, false);" in the HTML file. Rather this was called in another file.

Now I got it working. Thanks for all your time :)--
You received this message because you are subscribed to the GoogleGroups "phonegap" group.

To post to this group, send email to phonegap@googlegroups.com
To unsubscribe from this group, send email tophonegap+unsubscribe@googlegroups.com
For more options, visit this group athttp://groups.google.com/group/phonegap?hl=en?hl=en
For more info on PhoneGap or to download the code go to www.phonegap.com

[PhoneGap] Re: BlackBerry WebWorks - eclipse Issues

Hi,1. I'm using blackberry.app.exit() in my BB-Webworks App. and it is working for me.2. Here is the sample code for JS-WSDL communication. function wssample(){try{var oXmlHttp = new XMLHttpRequest(); // Create a function that will receive data sent from the serveroXmlHttp.onreadystatechange = function(){if(oXmlHttp.readyState == 4){alert(oXmlHttp.responseText);}};oXmlHttp.open("POST", "http://10.55.55.55/AuthenticationService.svc", false); oXmlHttp.setRequestHeader("Content-Type", "text/xml"); oXmlHttp.setRequestHeader("SOAPAction", "http://tempuri.org/IAuthenticationService/AuthenticateUser"); var str=" \ \ \ \ adminadmin \ admin \ \ \ \"; oXmlHttp.send(str); }catch(e){document.getElementById("ex").innerHTML = "Exception- "+e.toString(); }}Do not forget to add the url in config.xml-- You received this message because you are subscribed to the GoogleGroups "phonegap" group.To post to this group, send email to phonegap@googlegroups.comTo unsubscribe from this group, send email tophonegap+unsubscribe@googlegroups.comFor more options, visit this group athttp://groups.google.com/group/phonegap?hl=en?hl=en For more info on PhoneGap or to download the code go to www.phonegap.com

[PhoneGap] Re: how to add get applicationDidBecomeActive and didFinishLaunchingWithOptions methods in Phonegap

cd ~/Documents/phonegap-iphone/git remote updategit merge origin/mastermake, reinstall the pkg, reload, clean & rebuildOn Jun 30, 10:40 am, Abhilash Vijayakumar wrote:> Ok thank you. Any recommended way to convert an existing Phonegap.0.9.4 app> to Phonegap.0.9.6 app> Can Phonegap0.9.4 and 0.9.6 templates co-exist in xcode3.2.6 ?> Pls advice> Regards> Abhilash Vijayakumar>> On Wed, Jun 29, 2011 at 6:17 PM, Shazron Abdullah <>>>> shazron.abdul...@nitobi.com> wrote:> > Use the latest 0.9.6.> > applicationdidFinishLaunchingWithOptions is there,> > applicationDidBecomeActive is there but in the subclass PhoneGapDelegate.>> > For active/inactive events, use the "pause" and "resume" events in> > javascript instead to handle that event:> >http://docs.phonegap.com/phonegap_events_events.md.html>> > On 2011-06-29, at 3:00 AM, Abhi wrote:>> > > Hi,>> > > Native apps on IOS have the following methods> > > - (BOOL)application:(UIApplication *)application> > > didFinishLaunchingWithOptions:(NSDictionary *)launchOptions> > > - (void)applicationDidBecomeActive:(UIApplication *)application>> > > but Phonegap.0.9.4 has> > > - (void)applicationDidFinishLaunching:(UIApplication *)application> > > - (void)webViewDidFinishLoad:(UIWebView *)theWebView //this does not> > > trigger when app comes to foreground from background>> > > Pls advice how to add the two triggers in Phonegap platform (required> > > to implement push notification all scenarios)> > > - (BOOL)application:(UIApplication *)application> > > didFinishLaunchingWithOptions:(NSDictionary *)launchOptions> > > - (void)applicationDidBecomeActive:(UIApplication *)application>> > > --> > > You received this message because you are subscribed to the Google> > > Groups "phonegap" group.> > > To post to this group, send email to phonegap@googlegroups.com> > > To unsubscribe from this group, send email to> > > phonegap+unsubscribe@googlegroups.com> > > For more options, visit this group at> > >http://groups.google.com/group/phonegap?hl=en?hl=en>> > > For more info on PhoneGap or to download the code go towww.phonegap.com>> > --> > You received this message because you are subscribed to the Google> > Groups "phonegap" group.> > To post to this group, send email to phonegap@googlegroups.com> > To unsubscribe from this group, send email to> > phonegap+unsubscribe@googlegroups.com> > For more options, visit this group at> >http://groups.google.com/group/phonegap?hl=en?hl=en>> > For more info on PhoneGap or to download the code go towww.phonegap.com-- You received this message because you are subscribed to the GoogleGroups "phonegap" group.To post to this group, send email to phonegap@googlegroups.comTo unsubscribe from this group, send email tophonegap+unsubscribe@googlegroups.comFor more options, visit this group athttp://groups.google.com/group/phonegap?hl=en?hl=enFor more info on PhoneGap or to download the code go to www.phonegap.com

Twitter Delicious Facebook Digg Stumbleupon Favorites More