Over a million developers have joined DZone.
Platinum Partner

JavaScript: How to Read and Write local files in IE

In Web Applications, Reading local files in browsers is not common behavior.

· Web Dev Zone

The Web Dev Zone is brought to you in partnership with Mendix. Download this Forrester Report to gain a better understanding of the low-code platform market and how to make a strategic platform selection you won’t regret.

In Web Applications, Reading local files in browsers is not common behavior. Web Front End Developers can use this behavior to simulate the provision of Web Back End data. Then, we can implement parallel development of Web Front End and Web Back End. The development efficiency will also be improved.

The following code is about reading and writing local files in IE.

function readFileInIE(filePath) {
try {
var fso = new ActiveXObject("Scripting.FileSystemObject");

var file = fso.OpenTextFile(filePath, 1);

var fileContent = file.ReadAll();

file.Close();

return fileContent;
} catch (e) {
if (e.number == -2146827859) {
            alert('Unable to access local files due to browser security settings. ' + 
            'To overcome this, go to Tools->Internet Options->Security->Custom Level. ' + 
            'Find the setting for "Initialize and script ActiveX controls not marked as safe" and change it to "Enable" or "Prompt"'); 
        }
}
}

function writeFileInIE(filePath, fileContent) {
try {
var fso = new ActiveXObject("Scripting.FileSystemObject");

var file = fso.OpenTextFile(filePath, 2, true);

file.WriteLine(fileContent);

    file.Close();
} catch (e) {
if (e.number == -2146827859) {
            alert('Unable to access local files due to browser security settings. ' + 
            'To overcome this, go to Tools->Internet Options->Security->Custom Level. ' + 
            'Find the setting for "Initialize and script ActiveX controls not marked as safe" and change it to "Enable" or "Prompt"'); 
        }
}
}

From:  Jacky Cui's Java Home: Read and Write local files in IE

 

The Web Dev Zone is brought to you in partnership with Mendix. Better understand the aPaaS landscape and how the right platform can accelerate your software delivery cadence and capacity with the Gartner 2015 Magic Quadrant for Enterprise Application Platform as a Service.

Topics:
css,javascript,ie

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}