在 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