Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

html5 <input type="file" accept="image/*" capture="camera"> display as image rather than "choose file" button

Tags:

html

php

I've been looking into using html 5 <input type="file" accept="image/*" capture="camera"> to take a picture from my webapp and upload the image to the database using php - this is now working correctly.

However, the only way I can seem to find to display the "take picture" option is by a text field that has a button in it called "choose file"

Is there a way to be able to click on the existing image to open up the take picture options, then display the new image instead of the existing picture after the picture has been taken/file selected by the user? They should then click on the "upload" button if they are happy to save the image.

See JS fiddle here, hopefully this makes some sense! http://jsfiddle.net/6dxGY/

like image 828
Janey Avatar asked May 28 '14 16:05

Janey


People also ask

How do you make an input type file accept only images?

Using type="file" and accept="image/*" (or the format you want), allow the user to chose a file with specific format.

Why do you think it's best to include the accept attribute in an input type file?

The accept attribute is incredibly useful. It is a hint to browsers to only show files that are allowed for the current input .


1 Answers

You have to use Javascript Filereader for this. (Introduction into filereader-api: http://www.html5rocks.com/en/tutorials/file/dndfiles/)

Once the user have choose a image you can read the file-path of the chosen image and place it into your html.

Example:

<form id="form1" runat="server">     <input type='file' id="imgInp" />     <img id="blah" src="#" alt="your image" /> </form> 

Javascript:

function readURL(input) {     if (input.files && input.files[0]) {         var reader = new FileReader();          reader.onload = function (e) {             $('#blah').attr('src', e.target.result);         }          reader.readAsDataURL(input.files[0]);     } }  $("#imgInp").change(function(){     readURL(this); }); 
like image 192
mdunisch Avatar answered Sep 28 '22 18:09

mdunisch