Home > Web Front-end > JS Tutorial > JS creates a TXT text file with a specified path based on FileSystemObject_javascript skills

JS creates a TXT text file with a specified path based on FileSystemObject_javascript skills

WBOY
Release: 2016-05-16 15:47:08
Original
1232 people have browsed it

本文实例讲述了JS基于FileSystemObject创建一个指定路径的TXT文本文件。分享给大家供大家参考。具体如下:

Js创建一个指定路径的TXT文本文件,创建一个新的new ActiveXObject对象,使用这个对象的CreateTextFile方法来创建一个指定路径的TXT文本文件,简单吧?

在IE浏览器上运行该实例会有对应的安全提示,选择“允许阻止的内容”,使程序正常运行即可。如下图所示:

文本文件创建成功后提示如下:

若文本文件已存在,则有如下提示:

具体代码如下:

<html>
<head>
<title>创建文本文件</title>
</head>
<body>
<hr>
<script language="JavaScript"> 
var strFile = "c:\\test.txt";
var objFSO = new ActiveXObject("Scripting.FileSystemObject");
// 检查文件是否存在
if (!objFSO.FileExists(strFile)){
  // 创建文本文件
  var objStream = objFSO.CreateTextFile(strFile, true);
  document.write("创建文本文件: " + strFile + "<br>");
  objStream.Close(); // 关闭文件
}
else
  document.write("文本文件: " + strFile + "已经存在<br>");
</script>
</body>
</html>
Copy after login

希望本文所述对大家的javascript程序设计有所帮助。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template