在 Next.js 中上传图像是开发 Web 应用程序时的一项常见任务。在本教程中,我们将探索两种不同的方法:
第 1 步:在 Next.js 中创建上传表单
在pages/api/upload.js中创建文件:
import { useState } from 'react'; export default function UploadForm() { const [selectedFile, setSelectedFile] = useState(null); const handleFileChange = (event) => { setSelectedFile(event.target.files[0]); }; const handleSubmit = async (event) => { event.preventDefault(); const formData = new FormData(); formData.append('file', selectedFile); const response = await fetch('/api/upload', { method: 'POST', body: formData, }); if (response.ok) { console.log('File uploaded successfully'); } else { console.error('Error uploading file'); } }; return (); } 第三步:创建上传文件夹
import { useState } from 'react'; export default function UploadForm() { const [selectedFile, setSelectedFile] = useState(null); const handleFileChange = (event) => { setSelectedFile(event.target.files[0]); }; const handleSubmit = async (event) => { event.preventDefault(); const formData = new FormData(); formData.append('file', selectedFile); const response = await fetch('/api/upload', { method: 'POST', body: formData, }); if (response.ok) { console.log('File uploaded successfully'); } else { console.error('Error uploading file'); } }; return (); } 有了这个,您现在可以选择图像并将其直接上传到您的服务器。
3. 使用Filestack上传图片
Filestack 等外部服务,它提供文件托管、CDN 和高效的图像处理,请按照以下步骤操作。
第1步:安装Filestack
import { useState } from 'react'; export default function UploadForm() { const [selectedFile, setSelectedFile] = useState(null); const handleFileChange = (event) => { setSelectedFile(event.target.files[0]); }; const handleSubmit = async (event) => { event.preventDefault(); const formData = new FormData(); formData.append('file', selectedFile); const response = await fetch('/api/upload', { method: 'POST', body: formData, }); if (response.ok) { console.log('File uploaded successfully'); } else { console.error('Error uploading file'); } }; return (); } 第 2 步:在前端设置 Filestack
这是一个示例实现:
import { useState } from 'react'; export default function UploadForm() { const [selectedFile, setSelectedFile] = useState(null); const handleFileChange = (event) => { setSelectedFile(event.target.files[0]); }; const handleSubmit = async (event) => { event.preventDefault(); const formData = new FormData(); formData.append('file', selectedFile); const response = await fetch('/api/upload', { method: 'POST', body: formData, }); if (response.ok) { console.log('File uploaded successfully'); } else { console.error('Error uploading file'); } }; return (); } 第 3 步:获取 Filestack API 密钥
API 密钥。将上述代码中的 YOUR_API_KEY 替换为提供给您的密钥。
第四步:显示上传的图片
import { useState } from 'react'; export default function UploadForm() { const [selectedFile, setSelectedFile] = useState(null); const handleFileChange = (event) => { setSelectedFile(event.target.files[0]); }; const handleSubmit = async (event) => { event.preventDefault(); const formData = new FormData(); formData.append('file', selectedFile); const response = await fetch('/api/upload', { method: 'POST', body: formData, }); if (response.ok) { console.log('File uploaded successfully'); } else { console.error('Error uploading file'); } }; return (); } 4. 结论
免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3