0

I want to open the files located on local drive using window.open().

When i try to access the file using window.open i am getting error "Access is denied."

Would somebody help to achieve this requirement in Internet explorer 8.0?

Thanks!

0

2 Answers 2

2

You can't. And thank God for that. Imagine how insecure the internet would've been if JS was able to access a client's file-system.
Of course, IE8 has the MS specific JScript superset (ActiveXObject), which does enable filesystem access:

var fileHandle,
    fs = new ActiveXObject("Scripting.FileSystemObject");
fileHandle = fs.OpenTextFile("C:\\path\\to\\file.tmp", 1, true);
fileHandle.Write('This is written to a file');
console.log(fileHandle.ReadLine());//will log what we've just written to the file

But this is non-standard, is - I think- no longer supported either, and doesn't work X-browser.
Here's the documentation. At the bottom there's a link to a more detailed overview of the properties and methods this object has to offer, as you can see, there's a lot to choose from

7
  • How an we open using window.open method? I want to open the content using window.open. Window.open should open the content. Commented Apr 28, 2014 at 10:05
  • @user3463768: you can't. You can't open a file on the client's disk using window.open. The only thing you could do using window.open is window.open('file://<absolute path to file here>');. But there's no guarantee that'll work. JS is code that is sent to the client via the network. If it were allowed to access local files willy-nilly, then that would be incredibly unsafe. you really should look into other ways to do whatever it is you are trying to do Commented Apr 28, 2014 at 11:15
  • If you are trying to load an HTML file that is hosted by your machine, then window.open('http://127.0.0.1/dirname/file.ext') will work, but that requires you to have a server running Commented Apr 28, 2014 at 11:17
  • Is there any alternative for this? My files will be stored on local system. I am using HTML file upload control to read the file from local system and displaying the path in Grid when user clicks add button after browse from file system. So we show the path of the file in grid. User selects the path in grid and clicks on open button. The file should be opened or should ask for save. Any other way you think we can achieve this requirement? Commented Apr 28, 2014 at 11:36
  • @user3463768: Sounds like you're barking up the wrong tree with choosing JavaScript. You're talking about uploading files, which implies a server-side technology. I'd use AJAX + whatever server-side language you're using on the server Commented Apr 28, 2014 at 11:45
1

I'm adding this answer just to be complete, but so far as Web Pages go, Elias Van Ootegem's answer is correct: you can't (and shouldn't be able to) get to the local hard drive.

But .. you can isf your page is an HTA (HTML Application) :

HTML Application wiki

This is essentially a web page with .hta as the extension(usually) and some extra tags to tell IE that it's an HTA application, not a web page.

This is something that runs via the windows operating system and is so far as I'm aware only available for IE. The HTA application opens as a web page in IE, but without the usual web navigation / favourites toolbars etc.

Note that if you have a page on an internet server delivered as an HTA application, you're likely to cause virus scanners and firewalls to pop up because this would essenstially be running a script whcih could do manything to your computer. Not good for general internert stuff at all, but might be useful in a secure environment like an intranet where the source of the application is known to be safe.

To get to the file system, you can use javascript code like this :

// set up a Fils System Object variable.. 
var FSO = new ActiveXObject("Scripting.FileSystemObject");


// function to read a file
function ReadFile(sFile) {

var  f, ts;
var s="";
if(FSO.FileExists(sFile))
{
    f = FSO.GetFile(sFile); 
    ts = f.OpenAsTextStream(ForReading, TristateUseDefault); 
    if (!ts.AtEndOfStream) {s = ts.ReadAll( )}; 
    ts.Close( ); 
}
return s; 
}


alert(ReadFile("c:\\somefilename.txt");

Not the answer you're looking for? Browse other questions tagged or ask your own question.