Compatible with IE and Firefox, and reads local file JS Code

Source: Internet
Author: User

It is compatible with IE and Firefox and reads local file JS Code. The author does not provide examples, so only part of the code is available.

<Script type = "text/javascript">
Function read (file ){
If (typeof window. ActiveXObject! = 'Undefined '){
Var content = "";
Try {
Var fso = new ActiveXObject ("Scripting. FileSystemObject ");
Var reader = fso. openTextFile (file, 1 );
While (! Reader. AtEndofStream ){
Content + = reader. readline ();
Content + = "\ n ";
}
// Close the reader
Reader. close ();
}
Catch (e ){
Alert ("Internet too e read local file error: \ n" + e );
}
// VeryHuo. Com
Return content;
}
Else if (document. implementation & document. implementation. createDocument ){
Var content = ""
Try {
Netscape. security. PrivilegeManager. enablePrivilege ('universalxpconnect ');
Var lf = Components. classes ["@ mozilla.org/file/local1_1"]. createInstance (Components. interfaces. nsILocalFile );
Lf. initWithPath (file );
If (lf. exists () = false ){
Alert ("File does not exist ");
}

Var FCM = Components. classes ["@ mozilla.org/network/file-input-stream1_1"]. createInstance (Components. interfaces. nsIFileInputStream );
FCM. init (lf, 0x01,000 04, null );
Var sis = Components. classes ["@ mozilla.org/scriptableinputstream1_1"]. createInstance (Components. interfaces. nsIScriptableInputStream );
Sis. init (FCM );
Var converter = Components. classes ["@ mozilla.org/intl/scriptableunicodeconverter"]. createInstance (Components. interfaces. nsIScriptableUnicodeConverter );
Converter. charset = "UTF-8 ";
Content = converter. ConvertToUnicode (sis. read (sis. available ()));
}
Catch (e ){
Alert ("Mozilla Firefox read local file error: \ n" + e );
}

Return content;
}
}
</Script>

Related Article

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.