How to add data to file in javascript? - 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".

Related

creating a file by 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.

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

How to get the currently loading script name and the url variable of the script?

I am loading a script using the script embed tag and I want to get the url variables and the loading script name inside the script. Is it possible? For Example,
<script src="test.js?id=12"></script>
Inside test.js is there any way to get the url variable id?
Any help would be appreciated.
Thanks,
Karthik
Aside from the answers in the linked post, FWIW with Firefox 4 only you can (with caveats); document.currentScript.src which will return the full url, including arguments.
Thanks for all your efforts I have made that working by assigning an id attribute in the script tag and accessed via jQuery,
<script src="test.js?id=12" id="myScript"></script>
var currentScript = $("#myScript").attr('src'); //This will give me my script src
Thanks,
Karthik
If you want to get a variable from the current URL you can use this:
function queryParser(url){
this.get=function(p){return this.q[p];}
this.q={};
this.map=function(url){
url=url || window.location.search.substring(1);
var url=url.split('&');
var part;
for(var i=0;i<url.length;i++){
part=url[i].split('=');
this.q[part[0]]=part[1];
}
}
this.map(url);
}
var query=new queryParser();
// assuming you have ?test=something
alert(query.get('test'));
I recommend you map the result, so you don't re-parse whenever you want to find a specific element.
I don't really know why you'd pass a query string in a script tag like that, unless you specifically want off-site includes with a simple robust system for various effects. Or are actually using PHP to handle that request.
If you want to "send" a variable to one of your scripts, you can always do:
<script type="text/javascript">
var myVar="I'm in global scope, all scripts can access me";
</script>
<script src="test.js?id=12"></script>
If you really need to get the URL of the currently included script, you can use the code supplied by my peers in the other answers, you can then use:
var query=new queryParser(scriptURL);
alert(query.get('id'));// would alert 12 in your case
Navigating through the link on your comments you can get the proper answer.
Anyway, to make things easier:
var allScripts=document.getElementsByTagName('script');
var indexLastScript= allScripts.length -1;
alert (allScripts[indexLastScript].src);
This will show up "test.js?id=12" as a regular String so its up to you to split it in order to get de param.
Hope it helps, I've tried it on the run over the Chrome Javascript Console. :D.

HTML <input type="file"... check filesize with Flash

I'd like to know how to use the JavaScript onChange event on <input type="file"... so that when a user selected files, onChange will call Flash to return the file's size and decide what to do next.
2nd. How to use JavaScript to exclude this check, if the browser doesn't have Flash.
You can use a part of FancyUpload project - http://digitarald.de/project/fancyupload/. This control has a good documentation.
Alternatively I use the client ActiveX
var objFileSystem = new ActiveXObject("Scripting.FileSystemObject");
var strFilename = document.upload.file.value;
var resFile = objFileSystem.getFile(strFilename);
var intSize = resFile.size;
Check the Cross Browser Flash Detection in Javascript for 2nd issue

Create File with Javascript

is it possible to create a file on localhost with javascript?
Not in a webpage. If you're using Windows Script Host then yes you can through ActiveX, but I presume you're not doing that. You can however, send data back to the webserver through AJAX and have it store it for you.
You can create cookies to store data on the local machine, which pretty much is the only way to create files on the local machine.
I assume you have the content of the file ready. Then you can prompt a "save as" dialog like this:
var exportText; // this variable needs to contain your content
var targetFilename = "myfilename.ext"
function presentExportFile() {
var download = document.createElement('a');
// you need to change the contenttype to your needs in the next line.
download.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(exportText));
download.setAttribute('download', targetFilename);
download.style.display = 'none';
document.body.appendChild(download);
download.click();
document.body.removeChild(download);
}
2017 addendum: Since I wrote this, I had one exotic browser (xombrero) reject it. So, I can't say for certain that this is The Way.
no, this would be a security issue.
You can create a file through a plugin, see https://developer.mozilla.org/en/Code_snippets/File_I%2F%2FO
<html>
<head>
<title>Create File</title>
<! This function will create a file named 'newfile' on the same directory as the HTML unless path is given>
<script language="javascript">
function openFile()
{ var filePath = 'c:/filename.txt';
var fileSysObj = new ActiveXObject('Scripting.FileSystemObject');
fileSysObj.CreateTextFile(filePath);
}
</script>
</head>
<body>
This will create a file called "filename.txt" on your c:\ drive.
You must accept the ActiveX control or disable prompting to create a file.
<button type=submit name=button onClick="openFile();">create file</button>
</body>
</html>

Categories

Resources