Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

android file upload multipart/form-data

Can an Android app upload an image to the webserver? Does anyone have a definitive answer on this?

I notice file upload is not in the Android version of Kitchen Sink, but it seems like such a key functionality, I can't believe its missing?

I've managed to add the headers I need on Android to match the iPhone headers (which is successful) but there's nothing in the $_FILES array thats received by the webserver.

Much appreciate any ideas.

— asked August 21st 2010 by Leigh Kayley
  • android
  • file
  • image
  • multipart/form-data
  • upload
  • xhr
0 Comments

3 Answers

  • Okay, and for the final step, solving the poor quality image that gets uploaded. Firstly I place the image from event.media into an imageView where canScale = true and enableZoomControls = false.

    I then scale that imageView to the size I want to upload it at. Next I render that view as an image eg

    renderedImage = imageView.toImage();
    

    and use renderedImage as the image: for a new imageView. This step only seems to work if there is a slight time delay, I then render this new imageView as a blob:

    setTimeout(function(){        
    
    renderedImage = hiddenImageView.toImage();
    
    var saveImageView = Titanium.UI.createImageView({
    
    top: 0,
    left: 0,
    width: 'auto',
    height: 'auto',
    image: renderedImage,
    
    });
    
    imageScrollView.add(saveImageView);
    
    imageToUpload = saveImageView.toBlob();
    
    
    }, 500);
    

    and send the resulting data off to the server in the usual way:

    xhr.send({picture:imageToUpload,type:'submit'});
    

    On iPhone you need to set the content-type header to multipart/form-data on Android you don't (but this might just be for my particular case).

    The other thing I had to watch out for on the server is that from android the 'name' variable in the $_FILES array doesn't have an extension on it, so I have to tack on a .bin extension so it works with the rest of my php.

    It all seems a bit hackish (and I'd love to know if there's an easier way) but at least my image upload is working on iPhone and android… at least in the emulator anyway..

    — answered August 28th 2010 by Leigh Kayley
    permalink
    1 Comment
    • Not setting the content-type header to multipart/form-data on Android works! Great solution!

      — commented July 1st 2011 by Louis van de Wiele
  • For anyone else that's stuck on this the trick appears to be to convert the imageView to a blob with imageView.toBlob() as opposed to using toImage() as most of the code floating around this forum uses.

    Once done the $_FILES array will have content in it. The only problem now is the image quality is appalling. Will have to try and sort that out…

    — answered August 27th 2010 by Leigh Kayley
    permalink
    0 Comments
  • Leigh, I'm trying desperately to get image resize to work on Android. Here's what I've tried, based partially on your answer here:

    http://developer.appcelerator.com/question/64491/please-post-working-code-for-android-image-resize

    If you could take a look and offer advice or post complete working code I'd very much appreciate it.

    — answered September 24th 2010 by Parand Darugar
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.