input file多选时获取的文件字节与文件不对应的问题

      window.onload = function(){
        var input=document.getElementById("file");
        if ( typeof(FileReader) === ‘undefined‘ ){
        input.setAttribute( ‘disabled‘,‘disabled‘ );
        } else {
        input.addEventListener( ‘change‘,xmTanUploadImg,false );}
        }
         //选择图片,马上预览
    function xmTanUploadImg() {
     document.getElementById(‘validfile‘).value="0";
        var obj=document.getElementById("file");
        var fl = obj.files.length;
        for (var i = 0; i < fl; i++) {
            var file = obj.files[i];
            //这里我们判断下类型如果不是图片就返回 去掉就可以上传任意文件
                if(!/image\/\w+/.test(file.type)){
                alert("请确保文件为图像类型");
                return false;
                }
            var reader = new FileReader();

            //读取文件过程方法

            reader.onloadstart = function(e) {
                console.log("开始读取....");
            }
            reader.onprogress = function(e) {
                console.log("正在读取中....");
            }
            reader.onabort = function(e) {
                console.log("中断读取....");
            }
            reader.onerror = function(e) {
                console.log("读取异常....");
            }

            reader.readAsDataURL(file);
            reader.onload = function(e) {
                console.log("成功读取....");
                  var nDiv = document.getElementById(‘divnum‘).value;
                  document.getElementById(‘divnum‘).value = parseInt(nDiv) + 1;
                  var nValidFile = document.getElementById(‘validfile‘).value;
                  document.getElementById(‘validfile‘).value = parseInt(nValidFile) + 1;
                var strimgId = ‘img‘ + nDiv;
                var strfileId = ‘file‘ + nDiv;
                var strsizeId = ‘size‘ + nDiv;
                var strbzId = ‘bz‘ + nDiv;
                var strtplxId = ‘tplx‘ + nDiv;
                var strdivId = ‘div‘ + nDiv;
                var strbtnId = ‘btn‘ + nDiv;
                var strValueId=‘val‘ + nDiv;
                var strInputId=‘input‘ + nDiv;
                //直接根据索引获取文件字节会出现与其他图片大小错乱,故需要重新计算
                var base64url=this.result
                var str = base64url.replace(‘data:image/jpeg;base64,‘, ‘‘);//这里根据自己上传图片的格式进行相应修改
                str = str.replace(‘data:image/png;base64,‘, ‘‘);//这里根据自己上传图片的格式进行相应修改

                var strLength = str.length;
                var fileLength = parseInt(strLength - (strLength / 8) * 2);

                // 由字节转换为KB
                var size = "";
                size = parseInt(fileLength / 1024);
                document.getElementById("nSumFileSize").value=parseInt(document.getElementById("nSumFileSize").value)+size;
                var strhtm = ‘<div id="‘ + strdivId + ‘" class="PicDiv">‘;
                strhtm = strhtm + ‘<div style="float:left;"><img onerror="this.src=\‘/img/wfyl.gif\‘" style="cursor:pointer;" id="‘ + strimgId + ‘" src="‘+ e.target.result +‘" width=100 height=100 align=absmiddle ></div>‘;
                strhtm = strhtm + ‘<div  class="RightDiv"><p><span class="inputtitle">大小</span><input  class="inputnormal"  value="‘+size+‘kb" style="width:50px;vertical-align: middle;" readonly id="‘ + strsizeId + ‘" name="‘ + strsizeId + ‘">‘;
                strhtm = strhtm + ‘<span class="inputtitle">名称</span>‘;
                strhtm = strhtm + ‘<select class="inputnormal width_query_k"  id="‘+strtplxId+‘" name="‘+strtplxId+‘">‘;
                strhtm = strhtm + strtplxsels;
                strhtm = strhtm + ‘ </select>‘;
                strhtm = strhtm + ‘<span class="inputtitle">备注</span>‘;
                strhtm = strhtm + ‘<textarea id="‘+strInputId+‘" name="‘+strInputId+‘" style="display:none">‘+this.result+‘</textarea>‘;
                strhtm = strhtm + ‘<input class="inputnormal" style="width:200px;"  id="‘+strbzId+‘" name="‘+strbzId+‘"><input type="hidden" name="‘+strValueId+‘" id="‘+strValueId+‘" value="havevalue">‘;
                strhtm = strhtm + ‘<img src="/img/scbz1.gif" border=0 alt="删除" onclick="document.getElementById(\‘nSumFileSize\‘).value =parseInt(document.getElementById(\‘nSumFileSize\‘).value) -parseInt(document.getElementById(\‘‘+strsizeId+‘\‘).value);$(\‘#‘ + strdivId + ‘\‘).remove();$(\‘#‘ + strfileId + ‘\‘).remove();"></p>‘;
                strhtm = strhtm + ‘</div>‘;
                strhtm = strhtm + ‘</div>‘;
            document.getElementById(‘MyFiles‘).insertAdjacentHTML("beforeEnd", strhtm);
            }
        }

    }
reader.onload中直接通过file.files.size获取文件的字节会出现与其他图片的字节混乱的问题,故需要重新计算。

原文地址:https://www.cnblogs.com/wuchaofan1993/p/11445718.html

时间: 2024-07-30 15:14:20

input file多选时获取的文件字节与文件不对应的问题的相关文章

Windows - 你可以在Windows中做出一个全路径文件名(Fully Qualified File Name)长度超出MAX_PATH=260字节的文件

在任意一个目录下新建一个文件夹. 在该新建文件夹下创建任意一个文件:重命名该文件,使文件名长度不能再长为止. 重命名该新建文件夹,使该文件夹的名字长度更长. 综上,你就在Windows中做出了一个全路径文件名长度超出MAX_PATH=260字节的文件. Windows - 你可以在Windows中做出一个全路径文件名(Fully Qualified File Name)长度超出MAX_PATH=260字节的文件

【前端】input radio多选事件获取所有选中的id,radio样式优化可修改

$("#all_button").on('click', function() { obj = document.getElementsByClassName("input_radio_checked"); str = ""; for(i = 0; i < obj.length; i++) { str += obj[i].id + " "; //这里是数字之间的空格 } console.log(str) alert(st

项目(踩坑)_vue中使用input file类型的时候获取不到当前的inputFile对象

一.解决方法: <template>中 <script>中 原文地址:https://www.cnblogs.com/xxm980617/p/11367946.html

Dreamhost 提示No input file specified. 的解决的方法

假设开启FastCGI模式,.htaccess无法生效,一直提示no input file specified. 由于在Fastcgi模式下.php不支持rewrite的目标网址的PATH_INFO的解析 ThinkPHP执行在URL_MODEL=2时.会出现 No input file specified.的情况, 这时能够改动站点文件夹的.htaccess文件:(by default7#zbphp.com) RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]

使用java读取文件夹中文件的行数

使用java统计某文件夹下所有文件的行数 经理突然交代一个任务:要求统计某个文件夹下所有文件的行数.在网上查了一个多小时没有解决.后来心里不爽就决定自己写一个java类用来统计文件的行数,于是花了两个小时将代码写出(可见我的java功底还是挺烂的).虽然有很多有待改进的地方,依然有纪念意义. 本java类的核心是通过BufferedReader类的readLine()方法,间接的统计行数:通过递归遍历文件. 这个类只是写来完成任务的.结果不是很严谨,许多情况并没考虑到:比如判断想读取某一类文件怎

以input=file方式,获取文件时,在IE下8无法通过某个按钮click事件间接触发input=file的change事件

最近在项目中遇到了许多上传问题,公司内部的组件,通过form上传,需要使用input=file触发选择文件, 因为input=file在各个浏览器中显示的样式是不同的,谷歌,火狐,IE都有所不同,而且不太美观,所以为了解决 这一个问题,一开始采用了隐藏input=file 然后通过一个显示的button间接触发input=file的click事件如下 <input type="file" id="upload" style="display:none

input file实现多选和限制文件上传类型

<!-- input file  accept 属性设置可上传文件的类型  multiple属性设置可多文件上传--> <!-- accept 并未真正的实现限制上传文件类型,只是在选文件的时候有限制, 如果选文件的时候手动修改上传文件的类型,还是可以上传其他文件的. 因此,要实现只上传accept属性指定的文件类型,还必修结合js来实现--> <input type="file" accept=".doc,.docx" multipl

html,图片上传预览,input file获取文件等相关操作

input file常用方法: var obj=document.getElementById("upimage"); var file=obj.files[0];//获取文件数据 var path=obj.value;//获取文件当前路径 var size=obj.files[0].size;//获取文件大小 var prefix=path.substring( path.lastIndexOf('\\')+1 );//获取文件名的前缀名(文件格式) var suffix=path.

【Note2】用keil对工程编译时,工程中已经包含的文件会报错:cannot open source input file usb_conf.h No such file or directory

错误举例: ..\..\Libraries\STM32_USB-FS-Device_Driver\inc\usb_type.h(21): error:  #5: cannot open source input file "usb_conf.h": No such file or directory 这是因为没有设置 STM32 固件库的目录, 编译器就默认到 "Keil"根目录下的某某目录找去了.如果现在编译程序,会报错的 解决方法:引用固件库文件所在的目录需要在