Javascript Access to File on local machine - javascript

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!

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

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");

Related

How to move a file with JavaScript

I want to move a txt file from drive c to drive d and I found this code by searching but it does not work properly.
please guide me.
Thanks
<html>
<body>
<script language="JScript">
function move() {
var object = new ActiveXObject("Scripting.FileSystemObject");
var file = object.GetFile("C:\\1.txt");
file.Move("d:\\");
console.log("File is moved successfully");
}
</script>
<button onClick="move()">Move File txt</button>
</body>
</html>
Browsers do not provide any features that let code provided by a webpage move files the users' hard disks.
The code you've found may have worked in old versions of Internet Explorer (I think the feature was removed in later versions) but only when the security settings were altered from the default to allow it.
You could probably use it in server-side Classic ASP (but then it would move files on the server rather than the client).
For a browser-style UI which can do this, look to tools like Electron which pair a custom browser with Node.js in a desktop application. You can then use the Node.js side of your custom application to move files.
Obviously this will require that the user download and install your application and use that instead of their web browser.

Access Language JSON file with JS on Chrome without a webserver

tl/dr: How can I
internationalize strings in a html5/javascript application
while using a json file or something similar with key/value pairs (easy to translate)
without using javascript vars for every language string (ugly)
and if possible, without complex frameworks or packages
on Chrome (or something with same-origin-policy)
without a (local) webserver
without internet connection
Details:
I am developing a html5 touch game for older useres on an embedded IE system that will be changed to an embedded chrome system soon. Using a webserver is currently no option and I can't assume I have an internet connection all the time. Since the application should be in different languages, I currently have a json file that is accessed like this (irrelevant stuff left out):
//...
var language = "en"; //the language we want, same as the json file name
var key = "key"; //the key to the value we like to obtain
var languageMap;
var langFile = $.getJSON(language + ".json", function(data){
languageMap = data;
});
var langFileStatus = $.when(langFile);
langFileStatus.done(function () {
var value = languageMap[key];
//use the value of "key" here for awsome stuff
});
//...
the language file (e.g. "en.json") looks like this:
{
"key":"value",
"otherKey":"otherValue",
}
which works pretty well for IE and FF, but not on Chrome, because of the same-origin-policy. I read about an awsome trick to bypass that here, but I couldn't make it work in this case. I have never used JSON before in connection with JS, so maybe its an easy question. Different solutions for the whole problem are also very welcome (thats why I posted the complete problem). Thanks in advance!
Download Web Server for Chrome App from here. This is not exactly a server, but a handy simulation of the same that allows you to run your files locally as if they are running on a server.
It works without any Internet connection. More importantly, it has configuration options for CORS request thanks to recent updates Install it, select the folder in which your files are present, and you are ready! It's a really good way to test your code locally on Chrome.

Running executable application on Chrome?

Is it possible to run executable .exe application on Chrome browser or what option do I have?
I have seen example of JavaScript and it is desgined to work on IE because it use WScript.Shell (Not tested)
var ws = new ActiveXObject("WScript.Shell");
ws.run("C:\\System\\Display\\Display.exe \"" + message1 + "\" \"" + message2 + "\"");
So basically javascript it will execute Display.exe <Message>
Display.exe connect to COM3 (serial port) to display price on the Customer Display Pole (Till system)
Short: No it's not possible.
It's not even possible to call local files directly from chrome. It's really locked down in google chrome. If you manage to crack it you could strike it rich
In short, the best way to access local stuff is to set up a local webserver, call it, let the webserver execute a local file/protocol and then return the output to you via xhr or websockets.
Another option might be Java signed with secure certificates to allow some leeway, but even there the security measures are really tight.
Or you could make a chrome plugin and try Native Message Passing
Or, another option is that you fork chromium and build in your own activeX support into it. ChromiumX has a nice ring to it heh.
But all in all, it's really hard to get stuff done via chrome in what you want.
personally I resolved it by using PHP COM on a windows server to which I communicated via ajax requests to do the stuff I needed done, but it's less than ideal.

Deleting the file from client PC after upload but only after client permission

I got a requirement from client to delete file from his local pc after upload. It should ask to user if he want to delete the file after successful upload. If user opted yes then file should be deleted from client pc.
I know this is not easy to achieve in web application while quite easy in desktop app.My client is upgrading from desktop app to web and expecting the same behavior.
I heard that any browser plugin or small utility installed on client machine can do that. I have seen few example in other website that client is referring.
Can someone please suggest me the plugin or utility logic that can help me to achieve this? And how we can interact with these stuff from our java script or code.
Thanks in advance.
Regards,
Krishan
Javascript/HTML5 alone can't do this, it's restricted to maintain a certain level of security. You will have to look into activeX plugins, and it will only work if the user runs the web app on IE.
Here is a short example:
<script type="text/javascript">
// initialize ActiveXObject with Scripting.FileSystemObject:
var activeX_FileSystemObject = new ActiveXObject("Scripting.FileSystemObject");
if(confirm("Delete file?"))
{
activeX_FileSystemObject.DeleteFile("C:\\myFolder\\myFile.txt", true);
}
// Another way (multiple files in a catalog):
if(confirm("Delete file?"))
{
activeX_FileSystemObject.DeleteFile("C:\\myFolder\\*.txt", true);
}
activeX_FileSystemObject = null;
</script>
It's also possible that a Chrome plugin can do the same as the activeX, but never coded one myself - and it's also Chrome spesific.

To Access Notepad, calculator through asp.net

I m trying to open Notepad, Calculator in button click in asp.net with code behind C#. I tried with the code
System.Diagnostics.Process.Start("c:\\windows\\system32\\notepad.exe");
this is working fine in local system but not working in the Server. I even tried with the javascript
function executeCommands(inputparms)
{
alert('ff');
var oShell = new ActiveXObject("Shell.Application");
var commandtoRun = "C:\\Winnt\\Notepad.exe";
if (inputparms != "")
{
var commandParms = document.form1.filename.value;
}
oShell.ShellExecute(commandtoRun, commandParms, "", "open", "1");
}
even this is not working out. Can you please suggest me in on how to open the notepad application in the client end with out disturbing server notepad.
This can't be done. Imagine the security mess we'd be in if a web-page could run arbitrary programs on a client machine. Oh wait... ;-)
This is not possible (in general, though you could possibly get around with with various applets and browser plugins). In fact, I would be quite mortified if any web page could execute an arbitrary program on my computer.
You cannot do this. ASP.NET runs on the server and you cannot run programs on the client computer. The ActiveX object you have shown should work but only in IE and only after the user explicitly authorizes the execution of it. Also the location of notepad.exe might differ depending on the client (could be c:\windows, c:\winnt, ... and some clients running for example on Linux or MacOS don't have such executable)
What you are trying to achieve is not possible because of the nature of application in case of ASP.Net. The application will execute on server and will only send client side HTML to client. Even if your code is syntatically correct, it would open up the utilities on server itself.
This Can be possible by using below code on click of server button or Link.
System.Diagnostics.Process.Start("notepad.exe");
System.Diagnostics.Process.Start("C:\Windows\System32\calc.exe")
Works fine, though you may have to adjust settings on your browser. Be sure calc.exe is in the directory.

Categories