js读取文件fileReader

摘要:
<!DOCTYPEhtmlPUBLIC“-//W3C//DTDXHTML1.0过渡//EN”http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd“><htmlxmlns=”http://www.w3.org/1999/xhtml“xml:lang=”zh-CN“dir=”ltr“><hea”
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh-CN" dir="ltr">  
 <head>   
    
 </head>   
 <body>   
    <input type="file"   > <!-- 只能上传单个文件 -->  
    <input type="file"   multiple> <!-- 可以上传多个文件 -->  
<input type="file" accept="image/*"/> 设置上传文件类型,这样打开文件的时候会只出现符合该类型的文件。

或者列出详细的类型如:<input type="file" accept="image/x-png,image/gif,image/jpeg,image/bmp" />
    <script>  
        window.onload=function(){  
            var f = document.getElementById("file");  
            var fs = document.getElementById("files");  
              
            //this.files即获取input中上传的file对象 是个数组   
            f.onchange = function(){  
                //获取文件对象  
                var file = this.files[0];  
                //使用fileReader对文件对象进行操作  
                var reader = new FileReader();  
                //将文件读取为arrayBuffer  
                //reader.readAsArrayBuffer(file);  
                //reader.onload = function(){  
                //  console.log(reader.result);  
                //}  
                  
                  
                /*reader.readAsBinaryString(file);  
                reader.onload = function(){  
                    console.log(reader.result);  
                }  
                */  
                //用于图片显示不需要传入后台,reader.result的结果是base64编码数据,直接放入img的src中即可  
                reader.readAsDataURL(file);  
                reader.onload = function(){  
                    console.log(reader.result);  
                }  
            }  
              
              
            fs.onchange = function(){  
                  
            }  
        }  
    </script>  
 </body>  
</html>  

  

Valid Accept Types:

For CSV files (.csv), use: 

<input type="file" accept=".csv" />
For Excel Files 2003-2007 (.xls), use: 

<input type="file" accept="application/vnd.ms-excel" />
For Excel Files 2010 (.xlsx), use: 

<input type="file" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" />
For Text Files (.txt) use: 

<input type="file" accept="text/plain" />
For Image Files (.png/.jpg/etc), use: 

<input type="file" accept="image/*" />
For HTML Files (.htm,.html), use:

<input type="file" accept="text/html" />
For Video Files (.avi, .mpg, .mpeg, .mp4), use:

<input type="file" accept="video/*" />
For Audio Files (.mp3, .wav, etc), use:

<input type="file" accept="audio/*" />
For PDF Files, use:

<input type="file" accept=".pdf" /> 

有个缺点,在设置后浏览器打开选择文件窗口时会自动筛选的文件夹下所有符合设定类型的文件,造成文件窗口延迟一定时间。 

优化的方法是列出你需要的详细类型,比如:

For Image Files (.png/.jpg/etc), use: 

<input type="file" accept="image/x-png,image/gif,image/jpeg,image/bmp" />

这样打开的速度就快很多了

免责声明:文章转载自《js读取文件fileReader》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇在mac中用终端来运行.c文件idea golangd 配置go mod下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

JS控制SVG缩放+鼠标控制事件

话不多说,直接上代码吧,不行你砍我。。。 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>js控制SVG缩放</title> </head> <body>...

php解码js使用escape转码的函数

/** * 功能和js unescape函数,解码经过escape编码过的数据 * @param $str */ function unescape($str) { $ret = ''; $len = strlen($str); for ($i = 0; $i < $len; $i ++) {...

Android查询:模拟键盘鼠标事件(adb shell 实现)

1. 发送键盘事件: 命令格式1:adb shell input keyevent “value” 其中value以及对应的key code如下表所列: KeyEvent Value KEYCODE Comment 0 KEYCODE_UNKNOWN 1 KEYCODE_MENU 在SDK2.1的模拟器中命令失效,sendevent命令可行...

js打印隐藏的div,可自定义样式

这里是全部代码,可以直接运行。js需要自行导入 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title></title> <script src="js/...

Nginx基础配置

  Nginx的配置中,至少需要加载几个核心模块和一个事件模块。这些模块运行所支持的配置项被称为基本配置---其他模块执行时的依赖配置项。     本文主要记录基本配置项的用法,这里主要分四类来进行记录: 1.用于调试、定位问题的 2.正常运行的 3.优化性能的 4.事件类 在Nginx中有一些配置项,不需要显式配置,它们具有一个默认的值,...

JS判断鼠标移入元素的方向

最终效果 这里的关键主要是判断鼠标是从哪个方向进入和离开的 $("li").on("mouseenter mouseleave",function(e) { var w = this.offsetWidth; var h = this.offsetHeight; var x = e...