Showing posts with label picup app. Show all posts
Showing posts with label picup app. Show all posts

Tuesday, August 7, 2012

Picup App load image locally rather than using Imgur

A few months back I blogged about using the Picup app to grap iPhone/iPod/iPad pictures from their "i"-device to your website.

The part that has been bugging me was doing a remote upload to Imgur rather than a local upload to my server. I assume Imgur owns any image you upload to them which was a concern of mine.

So I spent some time yesterday trying to figure out my missing piece. I was relieved when I figured out that it was not my lack of brain power :) but rather the technology that I was using. I don't generally use classic ASP. I prefer ASP.NET/C# or PHP any day! With Classic ASP, you cannot easily upload files to your server by accessing the response.form. Instead, you have to download one of several 3rd party components to get the files, which then you have to register the dll blah blah blah.

However, on my server I happened to also have access to PHP. Out of frustration, I thought I will just try all my code on PHP to see if the upload will work. And it did...easily.

So to re-iterate the steps:

STEP 1. Create initial page with an input button, here is a short sample:
<html>
<header></header>
<body class="iphone" onLoad="loadMobile();" >
<form method="post" name="form1">

<input type="file" name="attach1" id="attach1"/>

</body></form></html>
STEP 2. Add script calls to that page.
<script type="text/javascript" src="js/picup.js"></script>
<script type="text/javascript" src="js/prototype.js"></script>
STEP 3. Add javascript function that sets up picup app settings. This function is loaded on the body "onLoad" see step 1. You could probably use jQuery to load this after document ready...
<SCRIPT TYPE="text/javascript">
function loadMobile(){

Picup.checkHash();
var url = escape('http://yourwebsite.com/uploadPhoto.asp');
var posturl = escape('http://yourwebsite.com/get-posted.php');

currentParams = {
'callbackURL': url, //the page called after upload.
'referrername'   : escape('mywebsitename'),
'referrerfavicon'  : escape('http://yourwebsite.com/images/favicon.ico'),
'purpose'               : escape('Select...message to user..'),
'debug'   : 'false',
'returnThumbnailDataURL': 'false',
'thumbnailSize'         : '80',
'postURL': posturl,    //the page that will upload the photo
'returnServerResponse':'true' //returns output of posturl
};

Picup.convertFileInput($('attach1'), currentParams);
}
</script>
STEP 4. Setup the "postURL" page, for me it is a simple PHP file. It outputs the name of the file as the response body so that my callback page can get the file name and update records accordingly.
<?php
if (isset($_FILES)){
 foreach ($_FILES as $key => $file){
  move_uploaded_file($file['tmp_name'], 'photos/'.$file['name']);
  echo $file['name'];
 }
}
?>
STEP 5. Setup the javascript on the callback page to grab the file name...and anything else that it needs.
<SCRIPT TYPE="text/javascript">
var fileName = unescape(getHashUrlVars()['serverResponse']);
var success = getHashUrlVars()['status'];

//parse hashed values from url
function getHashUrlVars(){
 var vars = [], hash;
 var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('&');
 for(var i = 0; i < hashes.length; i++)
 {
 hash = hashes[i].split('=');
 vars.push(hash[0]);
 vars[hash[0]] = hash[1];
 }
 return vars;
}

....process as needed

I hope this helps.

Thursday, March 29, 2012

Use Picup App to upload image to website from iPhone

To allow iPhone image uploads on the the classic, yes, I said, classic ASP site I was working I followed the following Picup App instructions posted at:

http://www.parorrey.com/blog/php-development/resolve-mobile-safari-greyed-out-file-field-issue-in-iphone-ipod-ipad-by-using-picup-app-to-upload-images/

For the most part, the instructions worked just fine. The only item I would add is that the blog post is not very clear in stating that the iPhone user must download the free Picup App onto their phone for the code that you write to work. It is mentioned in the comments.

It is also mentioned here: http://picupapp.com/best_practices.html

However, the image is posted rather than the words of the error so googling it might be hard to find. The error that indicates the app has not been installed is a generic window that says:
"Safari cannot open the page because the address is invalid"

I did change the code to work with my asp site. I could not get the document.observe to work. I think that is what the prototype.js file is for but it was no go.

Instead I put 'hardcoded' the body tag "class=iphone" and used the onLoad to call my loadMobile() function instead of using the document.observe:


function loadMobile(){

Picup.checkHash();
var element = document.getElementById('aid');
var x;

if (typeof(element) != 'undefined' && element != null)
x = element.value;

var url = escape('http://mysite.com/uploadContactPhotoI.asp?Assigneduserid=' + x);

<!-- Set some starter params -->
currentParams = {
'callbackURL' : url,
'referrername' : escape('mysite'),
'referrerfavicon' : escape('http://mysite.com/images/favicon.ico'),
'purpose' : escape('Select your photo for the our App.'),
'debug' : 'false',
'returnThumbnailDataURL': 'true',
'thumbnailSize' : '80'
};

Picup.convertFileInput($('attach1'), currentParams);
}



And the following is the code used on the callback page go pick up the values passed back by the Picup app hash url:
<SCRIPT TYPE="text/javascript">

var allVars = getHashUrlVars();

// Get all URL parameters
var imageURL = unescape(getHashUrlVars()['remoteImageURL']);
var success = getHashUrlVars()['status'];

function getHashUrlVars(){
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('#') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}

function onSubmitForm(){
var startIndex = 0;

startIndex = imageURL.indexOf('.com/');
startIndex = startIndex + 5;

var fileName = imageURL.substring(startIndex);

if(success.toUpperCase() != 'COMPLETE')
alert("We were unable to complete the request to upload the image, please try again.");
else
{

document.getElementById('fname').value = fileName;
document.getElementById('rurl').value = imageURL;
document.getElementById('uploadedImg').src = imageURL;

}
}

function showImage(){

if(typeof(imageURL) != "undefined"){

if(document.getElementById('uploadedImg') != null)
{
document.getElementById('uploadedImg').src = imageURL;
document.getElementById('uploadedImg').style.display="block";
document.getElementById('uploadedImg').style.visibility="visible";
document.UpdateContactPhoto.action = "uploadContactPhotoI.asp?imageUrl=" + imageURL;
}
}
}

</script>