• js+ajax获取文件大小


    顾名思义,通过JS和Ajax来获取上传文件的大小,在上传之前可以有个判断,对上传的文件有所控制,因为js控制文件大小(JS获取文件大小)有些问题,具体大家试下就知道了,在此整理了一下ajax的获取文件大小的方法,比较好用,再调试过程中,出现了c:/fakepath/ 的错误,也将解决方案罗列在下面,以供大家参考

    废话少说,代码如下

    JS如下:

    <script language="Jscript">
    function chksize(){
         
             var ticketType = "Oil";
            var file1=document.getElementById("txtfile");
            var v = file1.value;
          
    file1.select();

    var realpath = document.selection.createRange().text;

          
                var input = "<Record><Type>TicketLoanResult</Type><TicketType>" + realpath + "</TicketType></Record>";

             var objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");      
             objXmlHttp.Open("POST", "XMLHttpPost.aspx", false);
             
             objXmlHttp.Send(input);
             
             var returnXml = objXmlHttp.responseText;
             if(returnXml!=null)
             {
            
                returnXml=parseInt(returnXml/1024/1024);
              
               
             }
            alert(returnXml);
           return false;
    }
        </script>

    后台代码:

    public partial class XMLHttpPost : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                XmlDocument xmlDocumenet = new XmlDocument();

                xmlDocumenet.Load(Request.InputStream);

                string type = xmlDocumenet.SelectSingleNode("descendant::Type").InnerText;

                string returnMessage = string.Empty;

                switch (type.Trim())
                {
                    case "TicketLoanResult":
                        returnMessage = GetTicketLoanResult(xmlDocumenet);
                        break;
                    default:
                        break;
                }

                Response.ContentType = "text/xml";
                Response.Write(returnMessage);
            }
            catch (Exception exceptional)
            {
                //如果有錯誤則返回錯誤信息(Xml格式)
                string errorMessage = "<Error>" + exceptional.Message + "</Error>";
                Response.ContentType = "text/xml";
                Response.Write(errorMessage);
            }
            finally
            {
                Response.End();
            }
        }

        private string GetTicketLoanResult(XmlDocument input)
        {
            XmlNode item = input.SelectSingleNode("descendant::TicketType");
            string ticketType = item.InnerText.Trim();
            FileStream stream = new FileStream(ticketType,FileMode.Open);
            return stream.Length.ToString();
        }
    }

    整体代码下载如下:


    Js+ajax 获取文件大小源代码下载

    相关文章:

    1、JS获取文件大小:http://www.cnblogs.com/skylaugh/archive/2012/07/05/2577702.html

     2、IE浏览器上传文件时本地路径变成”C:\fakepath\”的问题http://www.cnblogs.com/skylaugh/archive/2012/07/05/2577736.html

  • 相关阅读:
    http状态大全
    goaccess 分析日志
    laravel 初识
    mac 安装wget
    python 16进制转成中文
    zeromq安装---php-zmq
    Mac Yosemite 10.10 编辑mac的配置
    Mac Yosemite 10.10 下利用 Launchctl 自启动 mysql
    MacBook设置终端命令行的颜色高亮显示
    XPath 常用语法札记
  • 原文地址:https://www.cnblogs.com/skylaugh/p/2577716.html
Copyright © 2020-2023  润新知