给我写一段 html+js 实现修改ini文件和读取的功能源码
时间: 2024-03-02 13:48:13 浏览: 28
很抱歉,由于安全性问题,Web应用程序应该避免直接修改INI文件。但是,你可以使用PHP(或其他后端语言)编写一个API来处理INI文件的读取和修改,并使用JavaScript将数据发送到该API。以下是一个简单的示例:
index.html:
```html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>INI文件编辑器</title>
</head>
<body>
<h1>INI文件编辑器</h1>
<h2>读取INI文件</h2>
<button onclick="loadIni()">加载INI文件</button>
<pre id="ini-data"></pre>
<h2>编辑INI文件</h2>
<form id="ini-form">
<label>Section名称:</label>
<input type="text" name="section_name" required>
<br>
<label>键名:</label>
<input type="text" name="key" required>
<br>
<label>键值:</label>
<input type="text" name="value" required>
<br>
<button type="submit">保存</button>
</form>
<div id="message"></div>
<script>
function loadIni() {
fetch('/api/load_ini.php')
.then(response => response.text())
.then(data => {
document.getElementById('ini-data').textContent = data;
document.getElementById('message').textContent = '';
})
.catch(error => {
document.getElementById('message').textContent = error.message;
});
}
document.getElementById('ini-form').addEventListener('submit', event => {
event.preventDefault();
const formData = new FormData(event.target);
const data = {
section_name: formData.get('section_name'),
key: formData.get('key'),
value: formData.get('value')
};
fetch('/api/save_ini.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => response.json())
.then(data => {
document.getElementById('message').textContent = data.message;
})
.catch(error => {
document.getElementById('message').textContent = error.message;
});
});
</script>
</body>
</html>
```
load_ini.php(读取INI文件):
```php
<?php
$ini_file = 'path/to/your/file.ini';
if (!file_exists($ini_file)) {
http_response_code(404);
echo 'File not found';
exit;
}
$ini_data = file_get_contents($ini_file);
if ($ini_data === false) {
http_response_code(500);
echo 'Failed to read file';
exit;
}
header('Content-Type: text/plain');
echo $ini_data;
```
save_ini.php(保存INI文件):
```php
<?php
$ini_file = 'path/to/your/file.ini';
if (!file_exists($ini_file)) {
http_response_code(404);
echo json_encode(array('message' => 'File not found'));
exit;
}
$data = json_decode(file_get_contents('php://input'), true);
if (!$data) {
http_response_code(400);
echo json_encode(array('message' => 'Invalid request'));
exit;
}
$section_name = $data['section_name'];
$key = $data['key'];
$value = $data['value'];
if (!$section_name || !$key || !$value) {
http_response_code(400);
echo json_encode(array('message' => 'Missing data'));
exit;
}
$ini_data = parse_ini_file($ini_file, true);
if (!$ini_data) {
http_response_code(500);
echo json_encode(array('message' => 'Failed to read INI file'));
exit;
}
if (!isset($ini_data[$section_name])) {
$ini_data[$section_name] = array();
}
$ini_data[$section_name][$key] = $value;
$ini_string = '';
foreach ($ini_data as $section_name => $section_values) {
$ini_string .= "[$section_name]\n";
foreach ($section_values as $key => $value) {
$ini_string .= "$key=$value\n";
}
}
if (file_put_contents($ini_file, $ini_string) === false) {
http_response_code(500);
echo json_encode(array('message' => 'Failed to write INI file'));
exit;
}
echo json_encode(array('message' => 'Data saved successfully'));
```
请注意,上述代码仅用于示例目的,实际应用程序需要进行更多的安全性和错误处理。