Take a Photo and Upload it on Mobile Phones with HTML5

In my daily life, I enjoy taking photos with my smartphone and uploading them to various websites. So I started thinking, “Is it possible to implement these functions in a web browser?” Fortunately, the new HTML5 SDK is capable of uploading local images or captured images to web servers. Everything becomes easier if we have a mobile web browser, like Chrome, which is fully compatible with HTML5.

Considering a Commerical Solution?

Dynamic Web TWAIN is a browser-based document scanning SDK specifically designed for web applications. It delivers a unified document capture and management experience across Windows, Linux, macOS, Android, and iOS.

Learn more about Web TWAIN >

On top of image capture, Dynamic Web TWAIN mobile edition supports auto border detection and crop, video stream embedding, and more.

Use HTML5 Built-in Functionality to Take Photos in Browsers on Android and iOS

Using HTML5 to invoke the camera is very simple.

For more information, you can reference HTML Media Capture. Due to the platform-dependency, you need to read Mobile HTML5 and search HTML Media Capture for relevant information. Referring to the article Html5 File Upload with Progress, I have made some improvements. The source code has been tested on Android 4.1 and iOS 7.0.6. You can see the following figures.




image capture image upload

For ASP.Net


<!DOCTYPE html>
    <title>Take or select photo(s) and upload</title>
    <script type="text/javascript">
      function fileSelected() {
        var count = document.getElementById('fileToUpload').files.length;
              document.getElementById('details').innerHTML = "";
              for (var index = 0; index < count; index ++)
                     var file = document.getElementById('fileToUpload').files\[index\];
                     var fileSize = 0;
                     if (file.size > 1024 * 1024)
                            fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';
                            fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';
                     document.getElementById('details').innerHTML += 'Name: ' + file.name + '<br>Size: ' + fileSize + '<br>Type: ' + file.type;
                     document.getElementById('details').innerHTML += '<p>';

      function uploadFile() {
        var fd = new FormData();
              var count = document.getElementById('fileToUpload').files.length;
              for (var index = 0; index < count; index ++)
                     var file = document.getElementById('fileToUpload').files\[index\];
                     fd.append(file.name, file);
        var xhr = new XMLHttpRequest();

        xhr.upload.addEventListener("progress", uploadProgress, false);
        xhr.addEventListener("load", uploadComplete, false);
        xhr.addEventListener("error", uploadFailed, false);
        xhr.addEventListener("abort", uploadCanceled, false);

        xhr.open("POST", "savetofile.aspx");

      function uploadProgress(evt) {
        if (evt.lengthComputable) {
          var percentComplete = Math.round(evt.loaded * 100 / evt.total);
          document.getElementById('progress').innerHTML = percentComplete.toString() + '%';
        else {
          document.getElementById('progress').innerHTML = 'unable to compute';
      function uploadComplete(evt) {
        /* This event is raised when the server send back a response */

      function uploadFailed(evt) {
        alert("There was an error attempting to upload the file.");

      function uploadCanceled(evt) {
        alert("The upload has been canceled by the user or the browser dropped the connection.");

  <form id="form1" enctype="multipart/form-data" method="post" action="Upload.aspx">
      <label for="fileToUpload">Take or select photo(s)</label><br />
      <input type="file" name="fileToUpload" id="fileToUpload" onchange="fileSelected();" accept="image/*" capture="camera" />
    <div id="details"></div>
      <input type="button" onclick="uploadFile()" value="Upload" />
    <div id="progress"></div>

Server savetofile.aspx:

<%@ Page Language="C#" %>
  HttpFileCollection files = HttpContext.Current.Request.Files;
  for (int index = 0; index < files.Count; index ++)
    HttpPostedFile uploadfile = files\[index\];
    // You must create "upload" sub folder under the wwwroot.
    uploadfile.SaveAs(Server.MapPath(".") + "\\upload\\" + uploadfile.FileName);
  HttpContext.Current.Response.Write("Upload successfully!");


Modify the client:

  1. Change
fd.append(file.name, file);


fd.append('myFile', file);
  1. Change
xhr.open("POST", "savetofile.aspx");


xhr.open("POST", "savetofile.php");

Create savetofile.php:

if (isset($_FILES\['myFile'\])) {
    // Example:
    move_uploaded_file($_FILES\['myFile'\]\['tmp_name'\], "uploads/" . $_FILES\['myFile'\]\['name'\]);
    echo 'successful';

Source Code