JAVASCRIPT FILE OPT
2010-11-24 16:25
10 查看
Are you looking for the ways to access the file system using JavaScript? If your JavaScript code could access local files of the visitor to your site, it would be a huge security problem. That's why no browsers would allow it...
How can you use JavaScript to access your local files and folders? Currently there are two ways to do it:
1. Using JavaScript extensions (runs from JavaScript Editor), or
2. Using a web page and ActiveX objects (Internet Explorer only)
Using ActiveX objects gives you many possibilities, but there are two distinct disadvantages:
You need a web page to run your JavaScript, and
ActiveX objects only work with the Internet Explorer browser.
When using extensions, all you need to do is select Build / Execute from the menu and let JavaScript Editor do the job.
2. Copy and paste the code below
3. Save the file as FileRead.js, and
4. Select Build / Execute from the menu.
Note: If you do not save the file, getScriptPath() below will return an empty string.
// This example shows file manipulation routines: it echoes
// the contents of itself (the script file).
// Created with Antechinus® JavaScript Editor
// Copyright© 2009 C Point Pty Ltd
fh = fopen(getScriptPath(), 0); // Open the file for reading
if(fh!=-1) // If the file has been successfully opened
{
length = flength(fh); // Get the length of the file
str = fread(fh, length); // Read in the entire file
fclose(fh); // Close the file
// Display the contents of the file
write(str);
}
2. Copy and paste the code below
3. Save the file as FolderExample.js, and
4. Select Build / Execute from the menu.
Note: if you do not save the file, getCurrentFolder() below will return an empty string.
// This example shows folder manipulation routines: it lists
// the contents of the current folder.
// Created with Antechinus® JavaScript Editor
// Copyright© 2009 C Point Pty Ltd
write("The contents of " + getCurrentFolder());
fileName = findFirstFile("*.*"); // Find the first file matching the filter
while(fileName.length)
{
write(fileName);
fileName = findNextFile(); // Find the next file matching the filter
}
1. Run JavaScript Editor
2. Copy and paste the code below
3. (Optional) Save the file as WriteFileExample.js, and
4. Select Build / Execute from the menu.
function WriteFile()
{
var fh = fopen("c://MyFile.txt", 3); // Open the file for writing
if(fh!=-1) // If the file has been successfully opened
{
var str = "Some text goes here...";
fwrite(fh, str); // Write the string to a file
fclose(fh); // Close the file
}
}
WriteFile();
2. Copy and paste the code below
3. Save the file as DriveList.htm, and
4. View the page using Internal Viewer or Internet Explorer
<HTML>
<HEAD>
<SCRIPT language=JavaScript>
function ShowAvailableDrives()
{
document.write(GetDriveList());
}
function GetDriveList()
{
var fso, s, n, e, x;
fso = new ActiveXObject("Scripting.FileSystemObject");
e = new Enumerator(fso.Drives);
s = "";
do
{
x = e.item();
s = s + x.DriveLetter;
s += ":- ";
if (x.DriveType == 3) n = x.ShareName;
else if (x.IsReady) n = x.VolumeName;
else n = "[Drive not ready]";
s += n + "<br>";
e.moveNext();
} while (!e.atEnd());
return(s);
}
</SCRIPT>
</HEAD>
<BODY>
<P>
<SCRIPT language=JavaScript> ShowAvailableDrives(); </SCRIPT>
</P>
</BODY>
</HTML>
In addition, you cannot run the code on its own, it needs to be a part of a web page or a stand-alone HTML Application (HTA).
1. Run JavaScript Editor
2. Copy and paste the code below
3. Save the file as WriteFileX.htm, and
4. View the page using Internal Viewer or Internet Explorer
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
function WriteFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.CreateTextFile("c://Test.txt", true);
fh.WriteLine("Some text goes here...");
fh.Close();
}
</SCRIPT>
</HEAD>
<BODY>
<P>
<SCRIPT language="JavaScript"> WriteFile(); </SCRIPT>
</P>
</BODY>
</HTML>
1、解决TypeError: Error #2007: 参数 blendMode 不能为空的went
原因:flex sdk的版本太低了,我换成3.4的,不再有这样的问题
JavaScript is a simple yet very powerful scripting language. Why not use your knowledge of JavaScript for batch processing of local files and other common tasks? Well, you can! Not through the Internet of course, but internally on your computer, or on your intranet if you have one. |
1. Using JavaScript extensions (runs from JavaScript Editor), or
2. Using a web page and ActiveX objects (Internet Explorer only)
Using ActiveX objects gives you many possibilities, but there are two distinct disadvantages:
You need a web page to run your JavaScript, and
ActiveX objects only work with the Internet Explorer browser.
When using extensions, all you need to do is select Build / Execute from the menu and let JavaScript Editor do the job.
Example 1 (using extensions): Reading a file
1. Run JavaScript Editor2. Copy and paste the code below
3. Save the file as FileRead.js, and
4. Select Build / Execute from the menu.
Note: If you do not save the file, getScriptPath() below will return an empty string.
// This example shows file manipulation routines: it echoes
// the contents of itself (the script file).
// Created with Antechinus® JavaScript Editor
// Copyright© 2009 C Point Pty Ltd
fh = fopen(getScriptPath(), 0); // Open the file for reading
if(fh!=-1) // If the file has been successfully opened
{
length = flength(fh); // Get the length of the file
str = fread(fh, length); // Read in the entire file
fclose(fh); // Close the file
// Display the contents of the file
write(str);
}
Example 2 (using extensions): Listing files in a folder
1. Run JavaScript Editor2. Copy and paste the code below
3. Save the file as FolderExample.js, and
4. Select Build / Execute from the menu.
Note: if you do not save the file, getCurrentFolder() below will return an empty string.
// This example shows folder manipulation routines: it lists
// the contents of the current folder.
// Created with Antechinus® JavaScript Editor
// Copyright© 2009 C Point Pty Ltd
write("The contents of " + getCurrentFolder());
fileName = findFirstFile("*.*"); // Find the first file matching the filter
while(fileName.length)
{
write(fileName);
fileName = findNextFile(); // Find the next file matching the filter
}
Example 3 (using extensions): Writing a file using JavaScript
Writing files using JavaScript and built-in extensions is straightforward: open the file for writing, write to a file and close a file.1. Run JavaScript Editor
2. Copy and paste the code below
3. (Optional) Save the file as WriteFileExample.js, and
4. Select Build / Execute from the menu.
function WriteFile()
{
var fh = fopen("c://MyFile.txt", 3); // Open the file for writing
if(fh!=-1) // If the file has been successfully opened
{
var str = "Some text goes here...";
fwrite(fh, str); // Write the string to a file
fclose(fh); // Close the file
}
}
WriteFile();
Example 4 (using ActiveX and a web page): Listing available drives
1. Run JavaScript Editor2. Copy and paste the code below
3. Save the file as DriveList.htm, and
4. View the page using Internal Viewer or Internet Explorer
<HTML>
<HEAD>
<SCRIPT language=JavaScript>
function ShowAvailableDrives()
{
document.write(GetDriveList());
}
function GetDriveList()
{
var fso, s, n, e, x;
fso = new ActiveXObject("Scripting.FileSystemObject");
e = new Enumerator(fso.Drives);
s = "";
do
{
x = e.item();
s = s + x.DriveLetter;
s += ":- ";
if (x.DriveType == 3) n = x.ShareName;
else if (x.IsReady) n = x.VolumeName;
else n = "[Drive not ready]";
s += n + "<br>";
e.moveNext();
} while (!e.atEnd());
return(s);
}
</SCRIPT>
</HEAD>
<BODY>
<P>
<SCRIPT language=JavaScript> ShowAvailableDrives(); </SCRIPT>
</P>
</BODY>
</HTML>
Example 5 (using ActiveX and a web page): Writing a file using JavaScript
Writing files via ActiveX is slightly more involved than using JavaScript Editor extensions: you create an instance of a FileSystemObject, create a file, write to it, and close it.In addition, you cannot run the code on its own, it needs to be a part of a web page or a stand-alone HTML Application (HTA).
1. Run JavaScript Editor
2. Copy and paste the code below
3. Save the file as WriteFileX.htm, and
4. View the page using Internal Viewer or Internet Explorer
<HTML>
<HEAD>
<SCRIPT language="JavaScript">
function WriteFile()
{
var fso = new ActiveXObject("Scripting.FileSystemObject");
var fh = fso.CreateTextFile("c://Test.txt", true);
fh.WriteLine("Some text goes here...");
fh.Close();
}
</SCRIPT>
</HEAD>
<BODY>
<P>
<SCRIPT language="JavaScript"> WriteFile(); </SCRIPT>
</P>
</BODY>
</HTML>
1、解决TypeError: Error #2007: 参数 blendMode 不能为空的went
原因:flex sdk的版本太低了,我换成3.4的,不再有这样的问题
相关文章推荐
- 用JavaScript验证日期格式
- javascript asp教程添加和修改
- JavaScript For Beginners
- 用JavaScript自动选择留言板
- JavaScript学习(二)——基本数据结构
- 如何用JavaScript调用Web服务——callService/useService
- 1.2.2 JavaScript代码的编写工具--UltraEdit(2)
- javascript basical knowledge
- 用JavaScript获取URL中的参数值
- [javascript]弹出可拖动的层
- JavaScript继承详解(一)
- Javascript中操作cookie
- Android高手进阶教程(二十)之---Android与JavaScript方法相互调用!
- JavaScript 合并排序
- 自我学习而已——javascript——变量,作用域和内存问题
- html 中的 javascript 全选/取消全选
- javascript 正则表达式
- Javascript学习7 - 脚本化浏览器窗口
- JavaScript对象系统深入剖析-4.Scope和Closure(闭包