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?

3
  • stackoverflow.com/questions/2935946/… Commented Jul 4, 2011 at 10:03
  • Thnx niky but I am not going to develop Android App. I need to do it within web html.
    – Dynamikus
    Commented Jul 4, 2011 at 11:22
  • I need to know wherever the <input type="file"> 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
    Commented Jul 4, 2011 at 11:33

5 Answers 5

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

5
  • I found w3.org spec, but it says May 9 2013 which is so new that I wonder if it is reliable ...
    – dsdsdsdsd
    Commented Jun 17, 2013 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. Commented Jan 20, 2014 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
    Commented Jun 24, 2014 at 12:17
  • As far as I remember, it depends on a phone. Unfortunately.
    – bullgare
    Commented Apr 15, 2015 at 19:21
  • Wow.. this fixed the issue in Samsung Android Device.. Tnx Man.:) +1
    – Developer
    Commented May 13, 2015 at 7:12
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.

11
  • Ok then this means green light to the project. Thnx Lukas
    – Dynamikus
    Commented Jul 4, 2011 at 19:06
  • Hm, according to my tests and the information on caniuse.com (caniuse.com/#search=HTML5 form features) this does not work at all.
    – Ridcully
    Commented Mar 21, 2012 at 9:59
  • @Ridcully please clarify that. Commented Mar 21, 2012 at 16:22
  • 7
    Well, simply create a html page with <input type="file" multiple />. 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
    Commented Apr 10, 2012 at 7:26
  • 1
    @Ridcully: still your comment is useful. +1 for the comment! Commented Jul 5, 2013 at 10:06
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.

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.