JS and the ActiveXObject object of IE browser and the application extension of FileSystemObject

Source: Internet
Author: User

ActiveXObject Object

Enables and returns a reference to an Automation object.
This object is used only to instantiate automation objects, and this object has no members.

Warning: This object is a Microsoft extension and is only supported in Internet Explorer and is not supported in Windows 8.x store apps.

Grammar:

New ActiveXObject (servername.typename[, location])

Parameters:
newObj: Must be selected. The name of the variable to which the ActiveXObject is assigned.
servername: Must be selected. The name of the application that provides the object.
typename: Must be selected. The type or class of the object to be created.
location: Optional. The name of the network server in which to create the object.

Note
An Automation server provides at least one object. For example, a word processing application might provide application objects, document objects, and toolbar objects.

You can identify the values on the host PC in the HKEY_CLASSES_ROOT registry key  servername.typename  . For example, here are some examples of values you can find here, depending on which program you installed:

    1. Excel.Application

    2. Excel.Chart

    3. Scripting.FileSystemObject

    4. Wscript.Shell

    5. Word.Document

Note: ActiveX There may be security issues with the object. To use ActiveXObject, you may need to adjust security settings in Internet Explorer in the relevant security zone. For example, for a local Intranet zone, you typically need to change the custom 对没有标记为安全的 ActiveX 控件进行初始化和脚本运行 setting to "".

To create an Automation object, assign the new ActiveXObject to the object variable:

var New ActiveXObject ("Excel.Application"); var New

This code launches the application that created the object (in this example, the Microsoft Excel worksheet). After you create an object, you can reference it in code by using a defined object variable. In the following example, object variables ExcelSheet and other Excel objects, including application objects and ActiveSheet.Cells collections, are used to access the properties and methods of the new object.

 //  make Excel visible through the Application object.  ExcelSheet.Application.Visible = true   //  Place some text in the first cell of the sheet.  ExcelSheet.ActiveSheet.Cells (a). 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 (); 

Requirements
Supported in the following document modes: Quirks, Internet Explorer 6 standard, Internet Explorer 7 standard, Internet Explorer 8 standard, Internet Explorer 9 standard, Internet Ex Plorer 10 Standard and Internet Explorer 11 standard. Not supported in Windows 8.x store apps.

FileSystemObject Object

In the IE browser to implement the operation of the file function, FileSystemobject you need to object.
Programming FileSystemObject with objects is simple, typically through the following steps: Creating FileSystemObject objects, applying related methods, accessing object-related properties.

Create a FileSystemObject object
var New ActiveXObject ("Scripting.FileSystemObject");

Once the above code is executed, the FSO becomes an FileSystemObject object instance.

Apply related methods

Once you have created an object instance, you can use the object's related methods. For example, use the CreateTextFile method to create a text file:

var New ActiveXObject ("Scripting.FileSystemObject");   var f1 = fso.createtextfile ("C:\myjstest.txt",true");  
Accessing object-related properties

To access the related properties of an object, the first thing to do is to establish a handle to the object, which GetDrive is implemented by the Get GetFolder Series method: Responsible for GetFile acquiring the drive information, getting the folder information, Responsible for obtaining file information. For example, when you point to the following code, F1 becomes a handle to the file C:est.txt:

var New ActiveXObject ("Scripting.FileSystemObject");   var f1 = fso. GetFile ("C:\myjstest.txt");  

Then, use F1 to access the related properties of the object. Like what:

var New ActiveXObject ("Scripting.FileSystemObject");   var f1 = fso. GetFile ("C:\myjstest.txt");  Alert (

But one thing to note: For objects created using the Create method, you no longer have to use the Get method to get an object handle, and the name of a handle created directly using the Create method can:

var New ActiveXObject ("Scripting.FileSystemObject");   var f1 = fso.createtextfile ("C:\myjstest.txt",true");  Alert ("File Last modified:" + F1. DateLastModified);  
Example 1. Get the size of the upload file

HTML code:

<type= "file"  ID= "FilePath"  onchange= " GetFileSize (This) "/>

JS Code:

//compatible with IE9 low version get file sizefunctionGetFileSize (obj) {varfilesize; if(obj.files) {filesize= Obj.files[0].size; }Else{        Try{            varPath,fso; Path= document.getElementById (' FilePath '). Value; FSO=NewActiveXObject ("Scripting.FileSystemObject"); FileSize=FSO.         GetFile (path). Size; }        Catch(e) {//in IE9 and low-version browsers, if you do not allow ActiveX controls to interact with the page, clicking No will not get the sizeConsole.log (E.message);//Automation Server cannot create objectFileSize = ' ERROR ';//cannot get        }    }    returnfilesize;}
2. Limit the types of uploaded files

In the case of a high-version browser, it is generally possible to write in HTML code, such as:

<type= "file"  name= "FilePath"  accept= ". jpg,. Jpeg,.doc,.docxs,.pdf ">

If you limit the upload file to a picture type, as follows:

<type= "File"  class= "file"  value= " Upload "  accept=" image/* "/>

But in other low-version browser is not used, need to judge JS.
HTML code:

<type= "file"  ID= "FilePath"  onchange= " Limittypes () "/>

JS Code:

/*verify the file format with the extension. * @parma filepath{string} file path * @parma Acceptformat{array} allowed file types * @result return value {Boolen}:true or False*/functionCheckformat (filepath,acceptformat) {varResultbool=false, ex= Filepath.substring (Filepath.lastindexof ('. ') + 1); Ex=ex.tolowercase ();  for(vari = 0; i < acceptformat.length; i++) {if(Acceptformat[i] = =ex) {Resultbool=true;  Break; }    }    returnResultbool;}; functionlimittypes () {varobj = document.getElementById (' FilePath '); varPath =Obj.value; varresult = Checkformat (path,[' bmp ', ' jpg ', ' jpeg ', ' PNG ')]); if(!result) {Alert (' Upload type error, please re-upload '); Obj.value= ' '; }}

Note: Of course This example 2 does not use the above knowledge here, just according to Example 1 Lenovo.

Reference

ActiveXObject Object (JavaScript): Https://msdn.microsoft.com/library/7sw4ddf8 (v=vs.94). aspx

JS, ActiveXObject, scripting.filesystemobject:http://www.cnblogs.com/dingjiaoyang/p/5831056.html

JS and the ActiveXObject object of IE browser and the application extension of FileSystemObject

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.