使用表单输入访问相机并使用网络应用程序立即上传照片 [英] Using form input to access camera and immediately upload photos using web app

查看:19
本文介绍了使用表单输入访问相机并使用网络应用程序立即上传照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个答案,非常棒:

在 iPhone iOS6 和 Android ICS 之后,HTML5 具有以下特性允许您从设备拍照的标签:

In iPhone iOS6 and from Android ICS onwards, HTML5 has the following tag which allows you to take pictures from your device:

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

Capture 可以获取相机、摄像机和音频等值.

Capture can take values like camera, camcorder and audio.

是否可以通过使用某种ajax在拍摄后立即上传照片来更进一步?

Is it possible to take this one step further by using ajax of some kind to immediately upload photo after its taken?

例如,使用我的手机,一旦我点击输入,它就会打开相机,这将立即让我拍照并保存它.当我将它保存到相机时,它会被输入按钮列为要上传的文件.

For example, using my phone, once I tap on the input, it then opens the camera which will immediately allow me to take a photo and save it. When I save it to camera, it's then listed by the input button as the file to upload.

如何立即上传这张照片而不是等待用户点击表单的提交按钮?

What would it take for this photo to be immediately uploaded instead of waiting for the user to click the Submit button of the form?

推荐答案

这真的很容易做到,只需通过文件输入的 onchange 处理程序内的 XHR 请求发送文件即可.

It's really easy to do this, simply send the file via an XHR request inside of the file input's onchange handler.

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

var myInput = document.getElementById('myFileInput');

function sendPic() {
    var file = myInput.files[0];

    // Send file here either by adding it to a `FormData` object 
    // and sending that via XHR, or by simply passing the file into 
    // the `send` method of an XHR instance.
}

myInput.addEventListener('change', sendPic, false);

这篇关于使用表单输入访问相机并使用网络应用程序立即上传照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆