写一个网页,网页有一个文件上传框和一个上传按钮,可以上传csv文件,上传后显示上传的csv文件内容
时间: 2024-05-25 13:17:29 浏览: 72
<!DOCTYPE html>
<html>
<head>
<title>CSV文件上传</title>
</head>
<body>
<h1>CSV文件上传</h1>
<form action="#" method="post" enctype="multipart/form-data">
<label for="csvFile">选择CSV文件:</label>
<input type="file" name="csvFile" id="csvFile"><br><br>
<input type="submit" value="上传文件">
</form>
<?php
if(isset($_FILES["csvFile"])) {
$fileName = $_FILES["csvFile"]["tmp_name"];
$file = fopen($fileName, "r");
echo "<table border='1'>";
while (($data = fgetcsv($file)) !== FALSE) {
echo "<tr>";
foreach ($data as $value) {
echo "<td>".$value."</td>";
}
echo "</tr>";
}
echo "</table>";
fclose($file);
}
?>
</body>
</html>
相关问题
写一个网页,网页有一个文件上传框和一个上传按钮,可以上传csv文件,上传后显示上传的csv文件内容,用js实现
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>CSV文件上传</title>
</head>
<body>
<h1>CSV文件上传</h1>
<input type="file" id="fileInput">
<button onclick="upload()">上传</button>
<br><br>
<div id="output"></div>
<script>
function upload() {
const input = document.getElementById('fileInput');
const file = input.files[0];
if (!file) {
alert('请选择文件');
return;
}
if (!file.name.endsWith('.csv')) {
alert('请选择CSV文件');
return;
}
const reader = new FileReader();
reader.onload = function() {
const data = reader.result;
const lines = data.split('\n');
const table = document.createElement('table');
for (let i = 0; i < lines.length; i++) {
const cells = lines[i].split(',');
const row = document.createElement('tr');
for (let j = 0; j < cells.length; j++) {
const cell = document.createElement('td');
cell.textContent = cells[j];
row.appendChild(cell);
}
table.appendChild(row);
}
const output = document.getElementById('output');
output.innerHTML = '';
output.appendChild(table);
};
reader.readAsText(file);
}
</script>
</body>
</html>
写一个网页,网页有一个文件上传框和一个上传按钮,可以上传csv文件,上传后显示上传的csv文件内容,如果文件超过指定行数,则提供翻页按钮,行数可以手工指定用js实现
<!DOCTYPE html>
<html>
<head>
<title>CSV文件上传</title>
<meta charset="UTF-8">
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<style type="text/css">
#table-wrapper {
margin-top: 50px;
display: none;
}
table {
border-collapse: collapse;
width: 100%;
}
table td, table th {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
table tr:nth-child(even) {
background-color: #f2f2f2;
}
table th {
padding-top: 12px;
padding-bottom: 12px;
background-color: #4CAF50;
color: white;
}
.pagination {
margin-top: 20px;
display: none;
overflow: hidden;
}
.pagination button {
background-color: #4CAF50;
color: white;
border: none;
padding: 10px 20px;
margin: 5px;
cursor: pointer;
float: left;
}
.pagination button.active {
background-color: #007bff;
}
</style>
</head>
<body>
<h1>CSV文件上传</h1>
<form id="form" enctype="multipart/form-data">
<input type="file" name="file" id="file">
<button type="submit" id="upload-btn">上传</button>
</form>
<div id="table-wrapper">
<h2>CSV文件内容</h2>
<table id="table"></table>
<div class="pagination">
<button id="prev-btn">« 上一页</button>
<button id="next-btn">下一页 »</button>
</div>
</div>
<script type="text/javascript">
var csvData = [];
var currentPage = 1;
var rowsPerPage = 10;
$(document).ready(function() {
$('#form').on('submit', function(event) {
event.preventDefault();
var file = $('#file')[0].files[0];
if (file) {
var reader = new FileReader();
reader.readAsText(file);
reader.onload = function(e) {
csvData = CSVToArray(e.target.result);
showTable();
}
}
});
$('#prev-btn').on('click', function() {
if (currentPage > 1) {
currentPage--;
showTable();
}
});
$('#next-btn').on('click', function() {
if (currentPage < getTotalPages()) {
currentPage++;
showTable();
}
});
$('#rows-per-page').on('change', function() {
rowsPerPage = parseInt($(this).val());
showTable();
});
});
function CSVToArray(csvString) {
var rows = csvString.split('\n');
var result = [];
for (var i = 0; i < rows.length; i++) {
var cells = rows[i].split(',');
if (cells.length > 0) {
result.push(cells);
}
}
return result;
}
function showTable() {
var table = $('#table');
table.empty();
var startIndex = (currentPage - 1) * rowsPerPage;
var endIndex = startIndex + rowsPerPage;
for (var i = startIndex; i < endIndex && i < csvData.length; i++) {
var row = csvData[i];
var tr = $('<tr></tr>');
for (var j = 0; j < row.length; j++) {
var td = $('<td></td>').text(row[j]);
tr.append(td);
}
table.append(tr);
}
$('.pagination').toggle(getTotalPages() > 1);
$('#prev-btn').toggleClass('active', currentPage > 1);
$('#next-btn').toggleClass('active', currentPage < getTotalPages());
}
function getTotalPages() {
return Math.ceil(csvData.length / rowsPerPage);
}
</script>
</body>
</html>
阅读全文