在此做一个笔记

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

<input type="file" accept="video/*" capture="camcorder">

<input type="file" accept="audio/*" capture="microphone">

accept表示打开的系统文件目录;

capture表示的是系统所捕获的默认设备(camera:照相机;camcorder:摄像机;microphone:录音);

multiple:支持多选,当支持多选时,multiple优先级大于capture;

再我做的一个红包活动中,需要笑脸照片来判断发红包,所以用到了input type="file"的标签属性,本来用的capture="camera"直接拍照,但是在ios上调相机是没有美颜功能的,担心用户不会选择上传,所以之后改变策略了,用了multiple,就是这样;

我的源代码:

<img src="" class="closebtnimg" id="closeimg1" />       //该标签是要放上传的照片的哈

<input type="file" accept="image/*" multiple  class="img"  onchange="javascript:setImagePreview('addimg1','closeimg1');" id="addimg1" />(错误的,有些手机不支持,底部有更新的正确的写法)

js:

var docObj;
    function setImagePreview(btnfile, imgId) {
         docObj = document.getElementById(btnfile);
         var imgObjPreview = document.getElementById(imgId);
         if (docObj.files && docObj.files[0]) {
                   imgObjPreview.style.display = 'block';
                   imgObjPreview.style.width = '100%';
                   imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]);
        } else {
                 imgObjPreview.style.display = 'block';
        }
      return true;
   }

以上是我的代码,很完美的哈,经过测试验证的。

2018-7-11更新调整

本来以上的代码是ok的,但是后期我用了大量的手机测试,有些手机是不成功的,所以上面的Html更新为最新的:

<input type="file" accept="image/*"  class="img"  onchange="javascript:setImagePreview('addimg1','closeimg1');" id="addimg1" />  (正确的,所有手机支持选择拍照,照片)

js-input file 文件上传(照片,视频,音频)的更多相关文章

  1. js 实现 input file 文件上传

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat=&qu ...

  2. input file 文件上传标签的样式美化

    input file 文件上传标签的样式美化 将<input type="file">的透明度设置为0: <input type="file" ...

  3. input file 文件上传,js控制上传文件的大小和格式

    文件上传一般是用jquery的uploadify,比较好用.后面会出文章介绍uploadify这个插件. 但是,有时候为了偷懒,直接就用input 的file进行文件和图片等的上传,input fil ...

  4. 修改input file 文件上传的样式

    Web页面中,在需要上传文件时基本都会用到<input type="file">元素,它的默认样式: chrome下: IE下: 不管是上面哪种,样式都比较简单,和很多 ...

  5. PHP 多input file文件上传

    前台html jquery代码 后台PHP处理 前台html <form id="form" method="post" enctype="mu ...

  6. input file文件上传样式

    <style>    .file-group {        position: relative;        width: 200px;        height: 80px;  ...

  7. js 实现 input type="file" 文件上传示例代码

    在开发中,文件上传必不可少但是它长得又丑.浏览的字样不能换,一般会让其隐藏点其他的标签(图片等)来时实现选择文件上传功能 在开发中,文件上传必不可少,<input type="file ...

  8. [置顶] js 实现 <input type="file" /> 文件上传

    在开发中,文件上传必不可少,<input type="file" /> 是常用的上传标签,但是它长得又丑.浏览的字样不能换,我们一般会用让,<input type ...

  9. input实现文件上传

    input实现文件上传 input + ajax 实现文件上传,包括文件大小及类型的判断 一.html <input type="file" id="file&qu ...

  10. js插件---IUpload文件上传插件(包括图片)

    js插件---IUpload文件上传插件(包括图片) 一.总结 一句话总结:上传插件找到真正上传位置的代码,这样就可以知道整个上传插件的逻辑了, 找资料还是github+官方 1.如何在js中找到真正 ...

随机推荐

  1. 16Shell脚本—计划任务服务程序

    计划任务服务程序 尽管我们现在已近有了功能彪悍的脚本程序来执行一些批处理工作,但是,如果仍然需要每天凌晨两点敲击键盘回车键来执行这个脚本程序,这简直太痛苦了.为此,我们需要学习如何设置服务器的计划任务 ...

  2. 转载:将画布(canvas)图像保存成本地图片的方法

    之前我曾介绍过如何将HTML5画布(canvas)内容转变成图片形式,方法十分简单.但后来我发现只将canvas内容转变成图片输出还不够,如何能将转变后的图片保存到本地呢? 其实,这个方法也是非常简单 ...

  3. tp5使用外部类的三种方法

    在tp5中使用外部类的时候有三种方法 第一种就是通过composer下载,通过这种方式下载的外部类能够支持自动加载,我们只要在使用的时候use一下命名空间就可以使用了 比如:我们的tp5第四季项目要使 ...

  4. OOP面向对象形式的初使化配置

    init.php里: <?php use ElemeOpenApi\Config\Config; define("BASE_DIR", dirname(__FILE__) . ...

  5. manjaro中virtualbox(vbox)配置

    一.更新源的配置: 1).自动方法: 在 终端 执行下面的arch" style="color: #002be5">命令从官方的源列表中对中国源进行测速和设置 su ...

  6. ubuntu gcc的下载链接,比较快的。

    http://mirrors.163.com/ubuntu-releases/ http://xhmikosr.1f0.de/tools/msys/

  7. AreYouBusy HDU - 3535 (dp)

    AreYouBusy Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  8. 线段树:CDOJ1592-An easy problem B (线段树的区间合并)

    An easy problem B Time Limit: 2000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) Pr ...

  9. 思维水题:UVa512-Spreadsheet Tracking

    Spreadsheet Tracking Data in spreadsheets are stored in cells, which are organized in rows (r) and c ...

  10. list_for_each_entry()函数分析

    list_for_each原型: #define list_for_each(pos, head) \ for (pos = (head)->next, prefetch(pos->nex ...