zoukankan      html  css  js  c++  java
  • IE浏览器的ActiveXObject对象以及FileSystemobject的应用扩展(完成)

    ActiveXObject 对象

    启用和返回对自动化对象的引用。
    此对象仅用于实例化自动化对象,且此对象没有成员。

    警告:此对象为 Microsoft 扩展,仅在 Internet Explorer 中受支持,在 Windows 8.x 应用商店应用中不受支持。

    语法:

    newObj = new ActiveXObject(servername.typename[, location])

    参数:
    newObj:必选。ActiveXObject 分配到的变量名称。 
    servername:必选。提供对象的应用程序的名称。 
    typename:必选。要创建的对象的类型或类。 
    location:可选。要在其中创建对象的网络服务器的名称。

    备注
    自动化服务器至少提供一种对象。 例如,字处理应用程序可能会提供应用程序对象、文档对象和工具栏对象。

    你可以在 HKEY_CLASSES_ROOT 注册表项中标识宿主 PC 上的 servername.typename 值。 例如,下面是可在此处找到的几个值示例,具体取决于安装的程序:

    1. Excel.Application

    2. Excel.Chart

    3. Scripting.FileSystemObject

    4. WScript.Shell

    5. Word.Document

    注意:ActiveX 对象可能存在安全问题。 若要使用 ActiveXObject,你可能需要在相关安全区域的 Internet Explorer 中调整安全设置。 例如,对于本地 Intranet 区域,通常需要将自定义设置更改为“对没有标记为安全的 ActiveX 控件进行初始化和脚本运行”。

    若要创建自动化对象,请将新的 ActiveXObject 分配给对象变量:

    var ExcelApp = new ActiveXObject("Excel.Application");
    var ExcelSheet = new ActiveXObject("Excel.Sheet"); 

    此代码启动创建对象的应用程序(在此示例中,为 Microsoft Excel 工作表)。 在创建某个对象后,可在代码中使用已定义的对象变量引用该对象。 在下面的示例中,使用对象变量 ExcelSheet 和其他 Excel 对象(包括应用程序对象和 ActiveSheet.Cells 集合)来访问新对象的属性和方法。

    // Make Excel visible through the Application object.
    ExcelSheet.Application.Visible = true;
    // Place some text in the first cell of the sheet.
    ExcelSheet.ActiveSheet.Cells(1,1).Value = "This is column A, row 1";
    // Save the sheet.
    ExcelSheet.SaveAs("C:\TEST.XLS");
    // Close Excel with the Quit method on the Application object.
    ExcelSheet.Application.Quit();

    要求
    在以下文档模式中受支持:Quirks、Internet Explorer 6 标准、Internet Explorer 7 标准、Internet Explorer 8 标准、Internet Explorer 9 标准、Internet Explorer 10 标准和 Internet Explorer 11 标准。 在 Windows 8.x 应用商店应用中不受支持。

    FileSystemObject 对象

    在IE浏览器中实现文件的操作功能,还得需要FileSystemobject对象。
    使用FileSystemObject 对象进行编程很简单,一般要经过如下的步骤: 创建FileSystemObject对象、应用相关方法、访问对象相关属性 。

    创建FileSystemObject对象

    var fso = new ActiveXObject("Scripting.FileSystemObject");

    上述代码执行后,fso就成为一个FileSystemObject对象实例。

    应用相关方法

    创建对象实例后,就可以使用对象的相关方法了。比如,使用CreateTextFile方法创建一个文本文件:

    var fso = new ActiveXObject("Scripting.FileSystemObject");  
    var f1 = fso.createtextfile("c:myjstest.txt",true");  

    访问对象相关属性

    要访问对象的相关属性,首先要建立指向对象的句柄,这就要通过get系列方法实现:GetDrive负责获取驱动器信息,GetFolder负责获取文件夹信息,GetFile负责获取文件信息。比如,指向下面的代码后,f1就成为指向文件c: est.txt的句柄:

    var fso = new ActiveXObject("Scripting.FileSystemObject");  
    var f1 = fso.GetFile("c:myjstest.txt");  

    然后,使用f1访问对象的相关属性。比如:

    var fso = new ActiveXObject("Scripting.FileSystemObject");  
    var f1 = fso.GetFile("c:myjstest.txt");  
    alert("File last modified: " + f1.DateLastModified); 

    但有一点请注意:对于使用create方法建立的对象,就不必再使用get方法获取对象句柄了,这时直接使用create方法建立的句柄名称就可以:

    var fso = new ActiveXObject("Scripting.FileSystemObject");  
    var f1 = fso.createtextfile("c:myjstest.txt",true");  
    alert("File last modified: " + f1.DateLastModified);  

    实例

    1.获取上传文件的大小

    html代码:

    <input type="file" id="filePath" onchange="getFileSize(this)"/>

    js代码:

    //兼容IE9低版本获取文件的大小
    function getFileSize(obj){
        var filesize;
        if(obj.files){
            filesize = obj.files[0].size;
        }else{
            try{
                var path,fso; 
                path = document.getElementById('filePath').value;
                fso = new ActiveXObject("Scripting.FileSystemObject"); 
                filesize = fso.GetFile(path).size; 
            }
            catch(e){
                //在IE9及低版本浏览器,如果不容许ActiveX控件与页面交互,点击了否,就无法获取size
                console.log(e.message); //Automation 服务器不能创建对象
                filesize = 'error'; //无法获取
            }
        }
        return filesize;
    }

    2.限制上传文件的类型

    如果是高版本浏览器,一般在HTML代码中写就能实现,如:

    <input type="file" name="filePath" accept=".jpg,.jpeg,.doc,.docxs,.pdf">

    如果限制上传文件为图片类型,如下:

    <input type="file" class="file" value="上传" accept="image/*"/>

    但是在其它低版本浏览器就不管用了,需要js来判断。
    html代码:

    <input type="file" id="filePath" onchange="limitTypes()"/>

    js代码:

    /* 通过扩展名,检验文件格式。
     *@param filePath{string} 文件路径
     *@param acceptFormat{Array} 允许的文件类型
     *@result 返回值{Boolen}:true or false
     */
    function checkFormat(filePath,acceptFormat){
        var resultBool= false,
            ex = filePath.substring(filePath.lastIndexOf('.') + 1);
            ex = ex.toLowerCase();
        for(var i = 0; i < acceptFormat.length; i++){
          if(acceptFormat[i] == ex){
                resultBool = true;
                break;
          }
        }
        return resultBool;
    };
            
    function limitTypes(){
        var obj = document.getElementById('filePath');
        var path = obj.value;
        var result = checkFormat(path,['bmp','jpg','jpeg','png']);
        if(!result){
            alert('上传类型错误,请重新上传');
            obj.value = '';
        }
    }

    注:当然这个实例2在这儿没有用到上面的知识,只是根据实例1联想到了。

    参考

    ActiveXObject 对象 (JavaScript):https://msdn.microsoft.com/library/7sw4ddf8(v=vs.94).aspx

    JS、ActiveXObject、Scripting.FileSystemObject:http://www.cnblogs.com/dingjiaoyang/p/5831056.html

  • 相关阅读:
    bzoj 4007
    bzoj 2190
    bzoj 2186
    bzoj 2005
    bzoj 2721
    bzoj 1951
    CF919F
    CF1005F
    CF1019C
    bitset用法详解
  • 原文地址:https://www.cnblogs.com/moqiutao/p/7253056.html
Copyright © 2011-2022 走看看