30

One of the crucial requirements in the application I am writing is that user being able to upload (input type="file") a photo from within a form.

Does the android web browser support File Uploads? If yes do all versions 1.5+ support it?

Kev
  • 118,037
  • 53
  • 300
  • 385
Dynamikus
  • 2,861
  • 4
  • 22
  • 20
  • http://stackoverflow.com/questions/2935946/sending-images-using-http-post – Nikunj Patel Jul 04 '11 at 10:03
  • Thnx niky but I am not going to develop Android App. I need to do it within web html. – Dynamikus Jul 04 '11 at 11:22
  • I need to know wherever the does work on Android devices, or if there is an HTML5 feature that allow from the webpage to browse the phone storage for files and then upload those from a form. In server side I know how to handle. – Dynamikus Jul 04 '11 at 11:33

5 Answers5

20

You can use this:

<input type="file" name="photo" accept="image/*" capture="camera">

The important thing is

capture="camera"

EDIT: as per lastest spec capture is a boolean attribute

aWebDeveloper
  • 36,687
  • 39
  • 170
  • 242
bullgare
  • 1,643
  • 1
  • 21
  • 32
  • I found [w3.org spec](http://www.w3.org/TR/html-media-capture/), but it says May 9 2013 which is so new that I wonder if it is reliable ... – dsdsdsdsd Jun 17 '13 at 12:50
  • I have a app that simply display a .html page within its window...this does not launch the file viewer to select a file on a tablet. – Rocco The Taco Jan 20 '14 at 16:42
  • Thank you for that amazing answer. But how can i display an capture option with an additional gallery option. With your version i only can caputure new pictures. ( Samsung Galaxy s4 is my test phone ) – bulleric Jun 24 '14 at 12:17
  • As far as I remember, it depends on a phone. Unfortunately. – bullgare Apr 15 '15 at 19:21
  • Wow.. this fixed the issue in Samsung Android Device.. Tnx Man.:) +1 – Developer May 13 '15 at 07:12
10

The correct format for Device API HTML input is:

<input type="file" name="photo" accept="image/*;capture=camera"></input>

This is supported by devices with Android 3.0 (for tablets) or Android 4.0 and later (for phones). I have no idea which version of iOS starts to support this.

Will
  • 1,989
  • 20
  • 19
10

Yes and no. Some people seem to have problems doing so (as outlined in the comments). Although it worked for all my devices, it's entirely possible that a different browser might not at all implement this feature.

The user can however not upload any file on the SD Card, but Audio-, Video- and Image-Files that are on the internal/external storage. The kind of files you can upload depend on the installed applications. If you have a File-Manager installed (or shipped with the OS), you can also use it to upload any file you want (Gallery and Mediaplayer should always be present).

When the upload-button of a <input type="file"> is pressed, browsers seem to send the Intent.ACTION_GET_CONTENT-Intent, so every application listening to this is a possible file-source.

Lukas Knuth
  • 25,449
  • 15
  • 83
  • 111
  • Ok then this means green light to the project. Thnx Lukas – Dynamikus Jul 04 '11 at 19:06
  • Hm, according to my tests and the information on caniuse.com (http://caniuse.com/#search=HTML5 form features) this does _not_ work at all. – Ridcully Mar 21 '12 at 09:59
  • @Ridcully please clarify that. – Lukas Knuth Mar 21 '12 at 16:22
  • 7
    Well, simply create a html page with ``. On a desktop browser like Chrome this allows you to select multiple files but on Android you can only select one file. I also found a comment from a developer of the Firefox for Android who explained that they do not support the `multiple` attribute, because there is no standard file-selector thingy in Android that would allow you to select more than one file. And on the mentioned caniuse.com page, scroll down to "HTML5 form features" and see that none (especially not multiple) are supported. – Ridcully Apr 10 '12 at 07:26
  • Hm, re-reading the original question, I think you didn't even ask for multiple uploads... Single uploads do indeed work. Sorry for the misunderstanding. – Ridcully Apr 10 '12 at 07:32
  • 1
    @Ridcully: still your comment is useful. +1 for the comment! – Pradip Kharbuja Jul 05 '13 at 10:06
  • Upload works in the Anroid browser viewing a standard HTML web form but NOT in a native android application that displays the same .html page. FYI. – Rocco The Taco Jan 20 '14 at 16:44
  • Tested today on http://cgi-lib.berkeley.edu/ex/fup.html, and built-in web browser on Android 4.4.2 does nothing when clicking Choose File button. So it is not working. – Pointer Null Feb 11 '14 at 22:03
  • @PointerNull I don't have a device with that version of the software. I do however have an Android 4.3 device and with it's version of Chrome (which is the default browser on this device) in works fine. – Lukas Knuth Feb 12 '14 at 12:07
  • @PointerNull Maybe this bug is relevant to your problem: https://code.google.com/p/android/issues/detail?id=62220 – Lukas Knuth Feb 12 '14 at 12:22
  • @Lukas Knuth: that's not really my problem, but browser's problem not providing full experience. Default browser on my device with Cyanogenmod 11 is those "Browser" app with blue globe icon. – Pointer Null Feb 12 '14 at 21:16
2

Yes, since Android 3.0 you are able to use device's camera through Device API. This snippet is taken from there

<form enctype="multipart/form-data" method="post">
  <h2>Regular file upload</h2>
  <input type="file"></input>

  <h2>capture=camera</h2>
  <input type="file" accept="image/*;capture=camera"></input>

  <h2>capture=camcorder</h2>
  <input type="file" accept="video/*;capture=camcorder"></input>

  <h2>capture=microphone</h2>
  <input type="file" accept="audio/*;capture=microphone"></input>
</form>

This source looks interesting when it comes to checking html5 support in mobile devices.

libnull-dev
  • 881
  • 1
  • 7
  • 19
1

I wanted an easier way to get files off my phone rather than pulling out the SD card and I thought to just have a CGI program receive them on a web server so I had the same question. I wrote a small script that can successfully upload files from remote computers using a web browser. It looks like this:

#!/usr/bin/python
import os
import cgi

def tag(tag, contents=None, attlist=None):
    tagstring= "<"+tag
    if attlist:
        for A in attlist:
            V= attlist[A].replace('"','&quot;')
            attstring= ' '+A+'="'+V+'"'
            tagstring += attstring
    if contents:
        tagstring += ">\n"+contents.rstrip()+"\n</"+tag+">\n"
    else:
        tagstring += "/>\n"
    return tagstring

content_type= 'Content-type: text/html\n\n'
form = cgi.FieldStorage()

if not form:
    acturl= "./up.py"
    ff= tag('input','',{'type':'file','name':'filename'}) + tag('input',''{'type':'submit'})
    f= tag('form',ff, {'action':acturl, 'method':'POST', 'enctype':'multipart/form-data'})            
    H= tag('head', tag('title', "Uploader"))
    B= tag('body', tag('p', f))
    print content_type + tag('html', H + B)
elif form.has_key("filename"):
    item = form["filename"]
    if item.file:
        data = item.file.read()  
        t= os.path.basename(item.filename)
        FILE= open("/home/user/public_html/uploads/"+t,'w')
        FILE.write(data)
        FILE.close
        msg= "Success! " 
    else:
        msg= "Fail. "

    H= tag('head', tag('title', "Uploader"))
    B= tag('body', tag('p', msg + tag('a','Another?',{'href':'./up.py'})))
    print content_type + tag('html', H + B)

Running a test with a program like this is the only sure way to know if your brand of phone browser does what you want, but for me: it worked. I was even able to use Apache mod_auth to require a username and password and the Android browser politely let me input that. Then when I selected the choose file button, it brought up a menu offering to let me choose from the Gallery, music ap, sound recorder, and a file manager ap I installed. I picked a file from the gallery and, although it took its sweet time, it uploaded fine. So the answer to the question for me was "yes". And for you - try out a test program like the one posted.

Chris XE
  • 11
  • 1