creating a file by javascript - javascript

i am trying to make a file in javascript, but it doesn't be saved in the root.
when the file is made, the sentence i write has to be stored in the file.
or loading a sentence from a textbox is also fine for me.
can anyone give me help??
function createFile() {
var fso = new ActiveXObject('Scripting.FileSystemObject');
var fileObj = fso.CreateTextFile("G:\\soonrok.txt", true);
fileObj.WriteLine("Hello, I am Soonrok!!!");
}
<td>
<input type="button" value="save" onClick="createFile()">

The best you're going to be able to do with JavaScript is create a cookie.
http://www.w3schools.com/js/js_cookies.asp
Or you could do something that only works for IE with ActiveX.
Or see the link from #AurA for Chrome Only tricks and/or ways to download files from a server.

Related

How To Set a .json file as a variable for use in window.open (javascript)?

I have made a website using html and javascript. I am Experienced In both, but am struggling with making a button open a website with the url being from a json file. So I want to make a json file a variable, then use it with window.open(JSON-url,"_blank"). I then want to make a var called JSON-url and it must be linked with the .json file.
I had Searched up many examples, but cannot seem to get the correct wordings. I have tried w3schools, Stack Overflow, Quora and many others. I mainly tried other searches for example, "How To Make JSON a variable to use in window.open javascript", but that didn't work either.
{
"JSON-url": "https://www.google.com"
}
I bet you there are a lot who know the answer to this, so please, raise your thoughts!
There's nothing special about JSON files when using a variable for the URL in window.open(). You do it the same as any other URL.
Use the FileReader API to read the file, then use the contents as the URL to open.
function openFile(files) {
if (files.length == 0) {
return;
}
const reader = new FileReader();
reader.onload = function() {
var url = reader.result.trim(); // Contents of the file
console.log("Opening " + url);
window.open(url, "_blank");
}
reader.readAsText(files[0]);
}
Select file:
<input type="file" id="txt" accept="text/plain" onchange="openFile(this.files)">
This won't work in the above snippet because Stack Snippets are sandboxed and don't allow window.open().

How to programmatically select local file in input type="file"

I want read a local txt file with javascript on chrome browser. So, I use <input type="file" .../> and when I select any txt file, I read it. But I dont want select file. I need to load the file with the file path. How is this possible?
Thanks
You can't do this. This is obviously because of security implications: imagine if any website you visit could read your FileZilla preferences file, which contains all your unencrypted FTP passwords? I bet you wouldn't like that.
You have to obtain a File reference (e.g. from an event handler) before being able to manipulate it. More info.
if you want to read file data opened from file dialog:
function readFile(){
var t = document.getElementById("file")
var o = new FileReader();
o.onload = function(t) {
console.log(t.target.result);
}
o.readAsText(t.files[0]);
}
edit: you can't just open files without selecting it first

How to add data to file in javascript?

I want to make registration form and write the data into data.txt using ActiveXObject.
What I write is
<script type="text/javascript">
function WriteFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.CreateTextFile("E:\\Test.txt", true);
x=document.getElementById("name").value;
y=document.getElementById("password").value;
fh.WriteLine(x+"#"+y);
fh.Close();
}
</script>
<BODY>
<form>
<input type="text" id="name"/>
<input type="text" id="password"/>
<input type="button" value="Sign Up" id="write" onclick="WriteFile()"/>
</form>
</BODY>
When I tried that way, every time I click Sign Up button, the new data override the previous data. I tried to use fh.AppendLine(x + "#" + y) but it didn't work.
Could anyone help me how to add data, not override data?
Disclaimer You should never use those functions. They only work in IE and are horrible.
I think your problem stems from using CreateTextFile. You should instead be using OpenTextFile with the second parameter set to 8. That will allow for appending.
I've done stuff like this a long time ago... (when I was using windows) I think it is because you're replacing the file with a new file with CreateTextFile so if the file already exists you'll need to do this:
function AppendLine()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.OpenTextFile("E:\\Training Asslab\\Advance\\Write to File\\Test.txt", 8, True);
x=document.getElementById("name").value;
y=document.getElementById("password").value;
fh.WriteLine(x+"#"+y);
fh.Close();
}
CreateTextFile overrwrites the current file, I think.
You should use FileExists to check its presence before creating it.
If it does exist you can use OpenTextFile.
Here's the relevant documentation
Use OpenTextFile method with create flag and ForAppending mode instead of CreateTextFile.
However, do understand, that you're not only limiting yourself to very old IE version and inside trusted zone, but you're also leaving files on user's local drives, not on your server. Because of that you can't do anything with this "registration data".

What is the equivalent of wget in javascript to download a file from a given url?

"wget http://www.example.com/file.doc" downloads that file to the local disk.
What is the equivalent of the above in javascript? for example, consider the following html snippet.
<html>
<head>
<script language="JavaScript">
function download_file() {
var url = "http://www.example.com/file.doc"
//
// Question:
//
// what should be done here to download
// the file in the url?
//
}
</script>
</head>
<body>
<input type="button" value="Download" onclick="download_file()">
</body>
</html>
Please suggest a solution that is compliant with all the browsers.
Sangeeth.
After a exploring more than a month, with a help of my friend, we were able to find out the following.
The website where the file is hosted is not allowing us to download the file using window.location = url; or window.open(url);
Finally we had to use the data-downloadurl support from HTML5 as follows
Click here to download the file
We embed this html into the host html and when clicked on the link, it triggers the download.
Why not use:
function download_file() {
var url = "http://www.example.com/file.doc"
window.location = url;
}
See https://developer.mozilla.org/en/DOM/window.location
If you need to open this in a new window/tab first then use:
function download_file() {
var url = "http://www.example.com/file.doc"
window.open(url);
}
See https://developer.mozilla.org/en/DOM/window.open
First thing that always comes in mind of every answerer to this question is executing wget shell command from java script.I'm almost certain that that's not possible because of
major security risk.
You pretty much need to have ajax which sends command to command line
either through php, or another scripting language via ajax...
You could probably make that happen with something like http://www.phantomjs.org/
I am saying probably because I read it from somewhere.

file Upload Verifier - jQuery/Javascript

Hiee,
I want to design a frontend of an Uploader page so that
only JPG file can be selected
Max file size should be 1mb
I want to do this check in JavaScript [not in PHP or so ...], can anyone help me ?
[I've no code to show]
Javascript solution i found.
If you want jquery only. convert it to jquery :-)
<html>
<head>
<script>
function getSize()
{
var myFSO = new ActiveXObject("Scripting.FileSystemObject");
var filepath = document.upload.file.value;
var thefile = myFSO.getFile(filepath);
var size = thefile.size;
alert(size + " bytes");
}
</script>
</head>
<body>
<form name="upload">
<input type="file" name="file">
<input type="button" value="Size?" onClick="getSize();">
</form>
</body>
</html>
It's unfriendly to users to insist firmly that filenames must indicate file type, but if you disagree with me you can check the filename on your <input> element. Checking actual file content and size is harder, and will require either Flash or new HTML5 features.
(The file name, stripped of any other path information and possibly even disguised with bogus path information, is available as the <input> element's "value" attribute.)
You may check the extensions (which does not check mime type!)
with something like this:
var el = document.getElementById('filename');
var fileName = el.value;
//do some regex magic to validate for /jpg\z/i
Also: You will also HAVE to check on the server. There's nothing stopping anyone with javascript disabled to upload .exe or other creepy large files.
For the rest I don't think there are many currently backwards compatible solutions for javascript. If you really want to do it client side, you might want to look into flash - since it has more permissions to check it. (Still you need to validate on the server side)
Another solution might be the html5 file api:
http://www.w3.org/TR/FileAPI/#dfn-file
examples with firefox >3.6:
https://developer.mozilla.org/en/using_files_from_web_applications
(Still you need to validate on the server)
i think u can use pluploader for your page.that can be handle your requirement very well.this is the link.http://www.plupload.com/example_queuewidget.php

Categories

Resources