Skip to content

Instantly share code, notes, and snippets.

@sebastianrothbucher
Last active August 29, 2015 14:17
Show Gist options
  • Save sebastianrothbucher/704f8e18289ee1d20450 to your computer and use it in GitHub Desktop.
Save sebastianrothbucher/704f8e18289ee1d20450 to your computer and use it in GitHub Desktop.
Image upload without phonegap/Cordova

There was a talk at conc.at about mobile with nothing but the browser. I didn't make it to conc.at (shame!) but I do care about the topic. So: there is a way, just google for

<input type="file" capture="camera"

This is a small sample to get it going on CouchDB - really hacky.

Steps (at your own risk - make sure you know what's going on!):

  1. get CouchDB
  2. make it listen to all clients (not just localhost) or use an Apache Webserver in front of it
  3. check out this gist
  4. run install.sh from this gist
  5. point your browser to http://server:port/couchfoto/_design/fkt/_show/foto/fotos
  6. Capture sth (on Desktop, it's just upload)
  7. Click the 'upload' button
  8. Go back and refresh (as said: still hacky)

Combine with appcache manifest, pouchdb / index storage, etc.

{"_id":"_design/fkt","shows":{"foto":"function(doc, req){\n /* very old-school but quick */\n var out=\"\";\n out+=\"<html>\";\n /* upload new foto */\n out+=\"<form method=\\\"post\\\" action=\\\"/couchfoto/fotos\\\" enctype=\\\"multipart/form-data\\\">\";\n out+=\"<input type=\\\"hidden\\\" name=\\\"_rev\\\" value=\\\"\"+doc._rev+\"\\\" />\";\n out+=\"<input type=\\\"file\\\" name=\\\"_attachments\\\" capture=\\\"camera\\\" accept=\\\"image/*\\\" />\";\n out+=\"<button type=\\\"submit\\\">Upload</button>\";\n out+=\"</form>\";\n /* list fotos */\n if(doc._attachments){\n for(var att in doc._attachments){\n out+=\"<p><img src=\\\"/couchfoto/fotos/\"+att+\"\\\" /></p>\";\n }\n }\n out+=\"</html>\";\n return out;\n}"}}
#!/bin/bash
curl -X PUT 'http://localhost:5984/couchfoto'
curl -X PUT 'http://localhost:5984/couchfoto/fotos' -d '{}'
curl -X PUT 'http://localhost:5984/couchfoto/_design/fkt' -d @design_fkt.json
# that's it
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment