Một hàm JavaScript được kích hoạt bởi sự kiện nhấn nút. Tôi sẽ chỉ cho bạn cách làm điều đó trong vài phút nữa! Để tạo một tệp văn bản từ javascript, chúng ta sẽ cần sử dụng đối tượng Blob.
Ví dụ
function WriteToFile(passForm) {
set fname = CreateObject("Scripting.FileSystemObject");
set solution = fname.CreateTextFile("<your Path>/pakainfo.txt", True);
var MemberUserName = document.getElementById('MemberUserName');
var memberSirname = document.getElementById('memberSirname');
solution.writeline("Member User Name :" + MemberUserName);
solution.writeline("Member Sir Name :" + memberSirname);
solution.writeline("-----------------------------");
solution.Close();
}
// HTML Source Code
<form onSubmit="WriteToFile(this)">
<label>Type your Member name:</label>
<input type="text" name="MemberUserName" id="MemberUserName" size="20">
<label>Type your Member sir name: </abel>
<input type="text" name="memberSirname" id="memberSirname" size="20">
<input type="submit" value="submit">
</form>
viết vào tệp javascript
Trước hết, cần Yêu cầu module fs trong đó hàm writeFile được định nghĩa. và Dữ liệu sẽ được ghi vào tệp và // Ghi dữ liệu vào ‘Results.txt’.
<script>
const fs = require('fs')
let data = "Welcome To Pakainfo.com free download source code."
fs.writeFile('Results.txt', data, (err) => {
// In case of a error throw err.
if (err) throw err;
})
</script>
viết vào tệp
var mainExFlData = "c:/product.txt";
var flName = new File(mainExFlData);
var userString = "Welcome To Pakainfo.com";
flName.open("w"); // open file with write access
flName.writeln("First line of text");
flName.writeln("Second line of text " + userString);
flName.write(userString);
flName.close();
đọc từ tệp
var mainExFlData = "c:/product.txt"
var flName = new File(mainExFlData);
flName.open("r"); // open file with read access
var userString = "";
while (!flName.eof) {
// read each line of text
userString += flName.readln() + "\n";
}
flName.close();
alert(userString);
writeTextFile viết dữ liệu vào tệp trên ổ cứng
function writeTextFile(filepath, output) {
var mainExFlData = new File(filepath);
mainExFlData.open("w"); //
mainExFlData.writeln(output);
mainExFlData.close();
}
readTextFile đọc dữ liệu từ tệp
function readTextFile(filepath) {
var userString = "";
var mainExFlData = new File(filepath);
mainExFlData.open("r");
while (!mainExFlData.eof) {
// read each line of text
userString += mainExFlData.readln() + "\n";
}
return userString;
}
JavaScript viết vào tệp văn bản
<html>
<h2>Create Text file in JavaScript</h2>
<script>
function createFile(){
var object = new ActiveXObject("Scripting.FileSystemObject");
var file = object.CreateTextFile("C:\\Welcome.txt", false);
file.WriteLine('Welcome To Pakainfo');
file.WriteLine('Free Download Source code for Pakainfo,com');
file.Close();
}
</script>
<input type="Button" value="Make File" onClick='createFile()'>
</html>
read-write-file.js
var mainExFlData = "c:/product.txt";
var flName = new File(mainExFlData);
var product_desc = "My string of text";
flName.open("w");
flName.writeln("First line of text");
flName.writeln("Second line of text " + product_desc);
flName.write(product_desc);
flName.close();
var mainExFlData = "c:/product.txt"
var flName = new File(mainExFlData);
flName.open("r"); // open file with read access
var product_desc = "";
while (!flName.eof) {
product_desc += flName.readln() + "\n";
}
flName.close();
alert(product_desc);
function writeTextFile(filepath, output) {
var mainExFlData = new File(filepath);
mainExFlData.open("w"); //
mainExFlData.writeln(output);
mainExFlData.close();
}
function readTextFile(filepath) {
var product_desc = "";
var mainExFlData = new File(filepath);
mainExFlData.open("r");
while (!mainExFlData.eof) {
// read each line of text
product_desc += mainExFlData.readln() + "\n";
}
return product_desc;
}
JavaScript Đọc và Viết vào Tệp Văn Bản
readFile(Path, Options, Callback)
writeFile:
writeFile(Path, Data, Callback)
Hy vọng bạn có một ý tưởng về javascript viết vào tệp văn bản.
- Bài viết blog này ban đầu được xuất bản tại: https://www.pakainfo.com