Jacky has posted 3 posts at DZone. View Full User Profile

JavaScript: How to Read and Write local files in IE

07.22.2011
| 18304 views |
  • submit to reddit

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

 

0
Your rating: None
Published at DZone with permission of its author, Jacky Cui.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags: