上傳圖片
小於 1 分鐘...
本教學指引你如何設定自己的圖床服務,以便在留言中上傳圖片。
@waline/client 提供了 imageUploader 選項。你應該自行處理客戶端傳入的圖片物件,在自行上傳圖片後,透過 Promise 回傳一個圖片位址。
案例
一個使用 lsky - pro 圖床的案例。
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Waline imageUploader 案例</title>
<link rel="stylesheet" href="https://unpkg.com/@waline/client@v3/dist/waline.css" />
</head>
<body>
<div id="waline" style="max-width: 800px; margin: 0 auto"></div>
<script type="module">
import { init } from 'https://unpkg.com/@waline/client@v3/dist/waline.js';
const waline = init({
el: '#waline',
serverURL: 'https://waline.vercel.app',
path: '/',
lang: 'en-US',
imageUploader: (file) => {
let formData = new FormData();
let headers = new Headers();
formData.append('file', file);
headers.append('Authorization', '!{API TOKEN}');
headers.append('Accept', 'application/json');
return fetch('!{API URL}', {
method: 'POST',
headers: headers,
body: formData,
})
.then((resp) => resp.json())
.then((resp) => resp.data.links.url);
},
});
</script>
</body>
</html>TIPS: 評論區僅作評論展示,如有問題諮詢請去 Github Discussion 中提問。
