失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > Take a Photo and Upload it on Mobile Phones with HTML5

Take a Photo and Upload it on Mobile Phones with HTML5

时间:2022-01-31 22:09:10

相关推荐

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 web browser?” Although DynamsoftImageCapture Suiteallows us to capture images fromwebcam, it is designed for a desktop App development on windows and Mac, not for mobile platforms. 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 to HTML5.

Take Photos in Browsers of Android and iOS

Using HTML5 to invoke the camera is very simple.

For more information, you can referenceHTML Media Capture. Due to the platform-dependency, you need to readMobile HTML5and searchHTML Media Capturefor relevant information. Referring to the articleHtml5 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.

Android:

Take a Photo and Upload it on Mobile Phones with HTML5

iOS:

Take a Photo and Upload it on Mobile Phones with HTML5 Take a Photo and Upload it on Mobile Phones with HTML5

Source Code

Client:

<!DOCTYPE html><html><head><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';elsefileSize = (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");xhr.send(fd);}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 */alert(evt.target.responseText);}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.");}</script></head><body><form id="form1" enctype="multipart/form-data" method="post" action="Upload.aspx"><div><label for="fileToUpload">Take or select photo(s)</label><br /><input type="file" name="fileToUpload" id="fileToUpload" οnchange="fileSelected();" accept="image/*" capture="camera" /></div><div id="details"></div><div><input type="button" οnclick="uploadFile()" value="Upload" /></div><div id="progress"></div></form></body></html>

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!");%>

For PHP

Modify the client:

1. Change

fd.append(file.name, file);

To

fd.append('myFile', file);

2. Change

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

To

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

Create savetofile.php:

<?phpif (isset($_FILES['myFile'])) {// Example:move_uploaded_file($_FILES['myFile']['tmp_name'], "uploads/" . $_FILES['myFile']['name']);echo 'successful';}?>

Source Code

Github:/DynamsoftRD/HTML5-Photo-Upload

Get the source code:git clone https:///DynamsoftRD/HTML5-Photo-Upload.git

原文:http://www.codepool.biz/take-a-photo-and-upload-it-on-mobile-phones-with-html5.html

转自:Take a Photo and Upload it on Mobile Phones with HTML5

如果觉得《Take a Photo and Upload it on Mobile Phones with HTML5》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。