您好,欢迎来到微智科技网。
搜索
您的当前位置:首页js截取文件名

js截取文件名

来源:微智科技网

  

js截取文件名

<script language="javascript" type="text/javascript">
       //取整个文件的路径并且把文件名赋给文件名
       var isTrue = true;
       function GetName(obj) {
           CheckType(obj);

           if (isTrue) {
               //隐藏控件赋值

               var hfExName = document.getElementById("<%=hfExName.ClientID %>");
               hfExName.value = GetFileName(obj.value);
              //服务控件文本框jq写法    id=   "txtName"   <Asp:TextBox />        

              $("#<%=txtName.ClientID %> input")[0].value = GetFileNameNoExt(obj.value);  // GetFileName(obj.value);
           }
       }
       //字符串逆转
       function strturn(str) {
           if (str != "") {
           var str1 = "";
           for (var i = str.length - 1; i >= 0; i--) {
               str1 += str.charAt(i);
           }
           return (str1);
           }
       }
       //文件类型
       function CheckType(obj) {

           if (obj.value.length > 0) {
               var af = "txt,docx,doc,docm,dotx,dotm,pptx,accdb,vsd,xlsx,xlsm,xltx,xlsb,xlam";
               if (eval("with(obj.value)if(!/" + af.split(",").join("|") + "/ig.test(substring(lastIndexOf('.')+1,length)))1;")) {
                   alert("支持上传文件类型:\n" + af);

                   isTrue = false;
               } else {
                   isTrue = true;

               }
           } else {

               isTrue = false;
           }
       }
       //取文件后缀名
       function GetFileExt(filepath) {
           if (filepath != "") {
               var pos = "." + filepath.replace(/.+\./, "");
               return pos;
           }
       }
       //取文件名不带后缀
       function GetFileNameNoExt(filepath) {
           var pos = strturn(GetFileExt(filepath));
           var file = strturn(filepath);
           var pos1 =strturn( file.replace(pos, ""));
           var pos2 = GetFileName(pos1);
           return pos2;

   <asp:FileUpload ID="fileUpload" runat="server" οnfοcus="GetName(this)"  />

js 2 更新

   <script language="javascript" type="text/javascript">
       //取整个文件的路径并且把文件名赋给文件名
       var isTrue = true;
       function GetName(obj) {
           CheckType(obj);

           if (isTrue) {
               var hfExName = document.getElementById("<%=hfExName.ClientID %>");
               hfExName.value = GetFileName(obj.value);
               $("#<%=txtStandardName.ClientID %> input")[0].value = GetFileNameNoExt(obj.value);  // GetFileName(obj.value);
               //document.getElementById("<%=txtStandardName.ClientID %>").value = GetFileNameNoExt(obj.value);
           }
       }
       //文件类型
       function CheckType(obj) {

           if (obj.value.length > 0) {
               var af = "txt,docx,doc,docm,dotx,dotm,pptx,accdb,vsd,xlsx,xlsm,xltx,xlsb,xlam";
               if (eval("with(obj.value)if(!/" + af.split(",").join("|") + "/ig.test(substring(lastIndexOf('.')+1,length)))1;")) {
                   alert("支持上传文件类型:\n" + af);

                   isTrue = false;
               } else {
                   isTrue = true;

               }
           } else {

               isTrue = false;
           }
       }
       //取文件后缀名
       function GetFileExt(filepath) {
           if (filepath != "") {
               var pos = "." + filepath.replace(/.+\./, "");
               return pos;
           }
       }
       //取文件名不带后缀
       function GetFileNameNoExt(filepath) {
           if (filepath != "") {
               var names = filepath.split("\\");
               var pos = names[names.length - 1].lastIndexOf(".");
               return names[names.length - 1].substring(0, pos);
           }
       }
       //取文件全名名称
       function GetFileName(filepath) {
           if (filepath != "") {
               var names = filepath.split("\\");
               return names[names.length - 1];
           }
       }       
   </script>

转载于:https://my.oschina.net/004/blog/1601

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- 7swz.com 版权所有 赣ICP备2024042798号-8

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务