How can I create a file using pure JavaScript? - javascript

I Got this code to create a file using JavaScript.
function WriteToFile()
{
var txt = new ActiveXObject("Scripting.FileSystemObject");
var s = txt.CreateTextFile("11.txt", true);
s.WriteLine('Hello');
s.Close();
}
WriteToFile();
it is working fine with IE but not Working in Chrome and Firefox.
usually the error is ReferenceError: ActiveXObject is not defined
is there any alternate to create file in javascript, instead of ActiveXobject(Scripting.FileSystemObject") ?

This isn't possible. At least not in pure JavaScript anyway, ActiveX objects are part of the Windows/IE thing and are NOT in the ES6 spec (http://www.ecma-international.org/ecma-262/6.0/index.html), which is why they're not supported in Chrome or Firefox.
If you wish to write JavaScript programs that can create files on the users local file system you're going to need to write a client side app, such as a chrome app (https://developer.chrome.com/apps/api_index) or write your program using Electron shell(http://electron.atom.io/).
However, if what you want is a cross browser solution to create files at specific locations on a user file system, it's just not really natively possible.
Sure browser plugins exist, but you'd need every user to install the plugin, so that's not exactly practical. For security reasons websites aren't allowed to read the users hard drive, and this is a good thing!

Related

How to execute a program from an unhosted HTML file without using Internet Explorer?

I'm attempting to create a front-end to launch several programs using HTML, CSS, and Javascript. My problem, however, is that the only ways that I can find to run a file either rely on Internet Explorer (Which I am not going to use) or download a new copy of the file.
Basically, I want to click a button or an image (Not 100% sure which one yet) and then run the program at the specified location. This isn't actually being hosted on a webserver; I'm just doing it because I make crappy GUIs in other languages, and HTML is comparatively easy.
This is also on Windows 7, if that has relevance.
I did something similar last year and used NW.js for it, it´s a webkit browser with integrated node.js functions.
It has its own executable and has access to your filesystem through node.
It was fun and easy to use, maybe give it a try.
If running your HTML files in a browser using the file:// protocol isn't an option, choose the technology you're the most comfortable with and look for a way to display webpages through it.
For instance you can have a look at:
WPF WebBrowser component (if you know a bit about .Net)
Java FX2 WebView (if you're more of a java guy)
etc...

ActiveX DLL using javascript

I want to access methods and properties on an Activex DLL that I have created in VB6
Just some back ground...
The VB6 DLL has the project name called PDFViewer and I have a single class clsPDFViewer.
In the class there are methods and properties that I wish to use.
I am using javascript with IE9.
Basically in javascript I have made a reference to the object
Attempted two ways
var PDFViewer = new ActiveXObject("PDFViewer");
var PDFViewer = document.getElementById("PDFViewer");
An error keeps occuring saying that it cannot create ActiveX
Even if I get past this error..
When i try something like this
alert(PDFViewer.Page);
then the other error is that the properties or methods do not exist
can someone please help
I am new to javascript.. please be patient
Its frustrating when I try one way is doesn't work for one reason
and then when I try another way is doesn't work for someother reason
grrrrrr...
This is due to security restrictions in IE.
The minimum dependency is that the DLL is registered on the client. (The VB6 IDE will do this if compiling on the same machine as testing, otherwise use the Rgsvr32 command to install the DLL on the client.)
Then you can either add the URL/domain hosting your page attempting to use the DLL to IE's "Trusted Sites" zone and/or you can enable ActiveX scripting for other internet zones.
(Although the latter will leave your browser in an unsafe state and constantly annoy you with messages about such!)

How to access file system in os by IE 7, and 8 browser ,without using ActiveXObject?

How to create a file or list the files in the folder in java script in IE7 and IE8.In general to access the file system in OS , java script uses ActiveXObject. But I need to access the file system not by ActiveXObject but by any other ways.
If I use ActiveXObject for access means,whenever access going to be happen each time a pop-up will appear that asks the user whether to allow or not ActiveXobject.It is little difficult one to client when dy faces this pop-up each time.
Is thr any Java script API exist to access the file system without use of ActiveXObject or any technique exist to do these things....?
I have to implement file system access applicaion in IE7 and IE8
If any one know kindly share ur knowledge.
Thanks in advance....
There are no ways in IE7/IE8 without browser plug-ins to access the local computer's file system because doing so is a major security risk and those browsers don't support any of the more modern ways of handling files.
You might get better ideas if you explain what actual problem you're really trying to solve rather than something as generic as your current question.
If this is an enterprise environment, you may be able to prewire some ActiveX settings in the enterprise browsers to allow your ActiveX plug-in to run without prompting.
Silverlight can have evaluated trust in browser applications in version 5
for First time it will ask user ( I've not tested it yet )
Notice that users must have installed Silverlight before then you can develop your own app by C#

EXE from JavaScript [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
How can I run a program or batch file on the client side?
I am new to JavaScript. I am trying to execute an EXE file from JavaScript on the web browers. How can it be done with simple code?
That is not possible due to security reasons. Imagine you are trying to access the file system of the client.
This is usually disallowed due to security reasons, if you absolutely need to do this then you have a few options however:
Prompt the user to download and run the executable
Use the WScript.Shell object
See this question for details on how to do this.
Note that this almost certainly won't work on most browsers unless your site has been placed into some sort of "trusted" zone (which may be the case if you are developing an intranet application or running inside a Html Application / .hta)
Use an ActiveX control (this will also require elevated permissions, however it is more likely that you will at least be able to prompt the user for permissions)
All 3 of these options require trust to be granted by the user (they will either have to have placed your page / site into a certain trust level, or they will have to have clicked on some sort of "trust this site" / "download this file" dialog.
(Also note that all of these options are speicfic to the Windows operating system)
As sAc says, it's not possible to directly run an exe file, though you may want to look into Google NaCl if you wish to do native code execution in a web browser. Also, please note that exe is a Windows format, and it would be very uninterpretable with other OSes and detrimental to the open web if you could run them through a browser.
It is possible if you lower the security level on your browser ..BUT that's not a good idea!
for example, in IE you can do this:
write a run function:
function run(file) {
var ws = new ActiveXObject("WScript.Shell");
ws.run(file);
}
and then for example: ....
onclick = run("file:///C:/Program%20Files/My%20Documents/yourFile.exe")

Is it possible to upload a whole folder instead of multiple files using Javascript?

Is it possible to upload a whole folder instead of multiple files using Javascript? If so, what is the approach. If there is no straighforward approach, is there any workaround to achieve the same? I have always been curious about this topic. Any insights will help.
No you can't, except if its zipped, or you can use flash, silverlight or applet for uploading more than one file in the same time.
Check these questions:
multiple file upload in just single browse click without jquery
Multiple File Selection For Uploading in ASP.NET
Edit:
For sure uploading multiple files at the same time is available now using html5 https://stackoverflow.com/search?q=multiple+file+upload+html5
EDIT
Turns out this is supported in Chrome 11, and seems to also work on Firefox if you use a vendor prefix:
https://stackoverflow.com/a/5849341/486547
This is definitely possible with a Java applet, however the % of folks with a JRE installed has gotten pretty low these days (< 70%)
Google Drive does this (choose upload, then "Folder..") It works for me (Mac OS X 10.7.5 in Chrome).
I'm not entirely sure how yet. I'm working on a project where this matters, so I've been looking for a while. I'll update as soon as I have more.
Sounds like you're already aware of this, but in the mean time, you can use valums or blueimp to do multiple file upload.
There is no way to do this in most browsers in javascript. Obviously though some (naive) browsers may have this functionality, as the javascript API is defined by the browser itself.
It may be possible using other technologies (such as silverlight, flash, etc) as mentioned in the other answers.
Of course there is. It opens a large security hole, but you can always use WScript and Filesystem ActiveX objects to access OS file system and transfer as many files as you like

Categories

Resources