html部分
<input
type="file"
accept="image/*"
capture="camera"
class="file"
id="preImage1"
@change="previewImage($event,'1')"
>
解析:
1. type="file" 上传类型,
2. ** accept**,这里先解析一下这个类型:
①.在文件上传中使用 accept 属性,本例中的输入字段可以接受 GIF 和 JPEG 两种图像:
②如果不限制图像的格式,可以写为:accept=“image/*”。
3. **capture:**如果没有这个 capture 属性,则需要先借助 MediaDevices.getUserMedia() 获取用户的相机权限,然后再通过 MediaRecorder API 进行录制,处理得到的文件。同样,对于拍照的情况也非常方便了,参考下方:
<input type="file" accept="image/*" capture="camera" />
<!-- or -->
<input type="file" accept="image/*;capture=camera"/>
浏览器兼容性
从 Can I Use - HTML Media Capture 的统计来看,支持得还不是很好。但尝试后发现 iOS 只能录制视频,即使指定 accept=“audio/*” 也是调起视频录制,而不是录音。