PPTX 是 Microsoft PowerPoint 的一种开放标准格式,用于存储演示文稿。它基于 Office Open XML 标准,是一种压缩的包格式,包含多个XML文件和其他资源(如图像、音频等)。
以下是一个简单的 HTML 和 JavaScript 示例,展示如何实现 PPTX 文件的上传功能:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Upload PPTX</title>
</head>
<body>
<input type="file" id="fileInput" accept=".pptx" />
<button onclick="uploadFile()">Upload</button>
<script>
async function uploadFile() {
const fileInput = document.getElementById('fileInput');
const file = fileInput.files[0];
if (file) {
const formData = new FormData();
formData.append('file', file);
try {
const response = await fetch('/upload', {
method: 'POST',
body: formData
});
if (response.ok) {
alert('File uploaded successfully!');
} else {
alert('Failed to upload file.');
}
} catch (error) {
console.error('Error uploading file:', error);
alert('There was an error uploading the file.');
}
} else {
alert('No file selected.');
}
}
</script>
</body>
</html>
假设你使用 Node.js 和 Express 来处理文件上传:
const express = require('express');
const multer = require('multer');
const path = require('path');
const app = express();
const upload = multer({ dest: 'uploads/' });
app.post('/upload', upload.single('file'), (req, res) => {
if (!req.file) {
return res.status(400).send('No file uploaded.');
}
const filePath = req.file.path;
console.log(`File saved to: ${filePath}`);
res.status(200).send('File uploaded successfully.');
});
app.listen(3000, () => {
console.log('Server started on http://localhost:3000');
});
通过以上步骤,你可以实现一个基本的 PPTX 文件上传功能,并确保文件的类型和大小符合要求。
领取专属 10元无门槛券
手把手带您无忧上云