40 lines
912 B
TypeScript
40 lines
912 B
TypeScript
import fs from "fs";
|
||
|
||
// 1️⃣ File yang mau diupload
|
||
const filePath = "image.png";
|
||
const apiUrl = "http://localhost:3000/api/pengaduan/upload-base64";
|
||
|
||
// 2️⃣ Baca file dan ubah ke base64
|
||
const fileBuffer = fs.readFileSync(filePath);
|
||
const base64Data = fileBuffer.toString("base64");
|
||
|
||
// 3️⃣ Buat payload JSON
|
||
const payload = {
|
||
data: base64Data,
|
||
mimetype: "image/png"
|
||
};
|
||
|
||
// 4️⃣ Kirim ke server pakai fetch
|
||
async function uploadBase64() {
|
||
try {
|
||
const res = await fetch(apiUrl, {
|
||
method: "POST",
|
||
headers: {
|
||
"Content-Type": "application/json",
|
||
},
|
||
body: JSON.stringify(payload),
|
||
});
|
||
|
||
if (!res.ok) {
|
||
throw new Error(`Request failed: ${res.status} ${res.statusText}`);
|
||
}
|
||
|
||
const result = await res.json();
|
||
console.log("✅ Upload sukses:", result);
|
||
} catch (err) {
|
||
console.error("❌ Upload gagal:", err);
|
||
}
|
||
}
|
||
|
||
uploadBase64();
|