working file upload
This commit is contained in:
parent
787ff9741e
commit
a0885013bb
6 changed files with 66 additions and 15 deletions
3
.gitignore
vendored
3
.gitignore
vendored
|
|
@ -40,3 +40,6 @@ yarn-error.log*
|
||||||
**/*.log
|
**/*.log
|
||||||
package-lock.json
|
package-lock.json
|
||||||
**/*.bun
|
**/*.bun
|
||||||
|
/src/uploads
|
||||||
|
/uploads
|
||||||
|
/mydb.sqlite
|
||||||
BIN
bun.lockb
BIN
bun.lockb
Binary file not shown.
|
|
@ -8,7 +8,8 @@
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@elysiajs/html": "^1.0.2",
|
"@elysiajs/html": "^1.0.2",
|
||||||
"@elysiajs/static": "^1.0.2",
|
"@elysiajs/static": "^1.0.2",
|
||||||
"elysia": "latest"
|
"elysia": "latest",
|
||||||
|
"nanoid": "^5.0.7"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"bun-types": "latest"
|
"bun-types": "latest"
|
||||||
|
|
|
||||||
36
src/index.ts
36
src/index.ts
|
|
@ -1,15 +1,39 @@
|
||||||
import { Elysia } from "elysia";
|
import { Elysia } from "elysia";
|
||||||
import { staticPlugin } from '@elysiajs/static'
|
import { staticPlugin } from "@elysiajs/static";
|
||||||
import { html } from '@elysiajs/html'
|
import { html } from "@elysiajs/html";
|
||||||
|
import { Database } from "bun:sqlite";
|
||||||
|
|
||||||
|
const db = new Database("./mydb.sqlite");
|
||||||
|
const baseDir = import.meta.dir;
|
||||||
|
const uploadsDir = "./uploads/";
|
||||||
|
|
||||||
const app = new Elysia()
|
const app = new Elysia()
|
||||||
.use(html())
|
.use(html())
|
||||||
.use(staticPlugin({
|
.use(
|
||||||
assets: "src/public/", prefix: "/"
|
staticPlugin({
|
||||||
}))
|
assets: "src/public/",
|
||||||
|
prefix: "/",
|
||||||
|
}),
|
||||||
|
)
|
||||||
.get("/", () => Bun.file("src/pages/index.html"))
|
.get("/", () => Bun.file("src/pages/index.html"))
|
||||||
|
.post("/upload", async (ctx) => {
|
||||||
|
console.log(ctx.body);
|
||||||
|
if (ctx.body?.file) {
|
||||||
|
await Bun.write(`${uploadsDir}${ctx.body.file.name}`, ctx.body.file);
|
||||||
|
} else if (ctx.body?.files) {
|
||||||
|
if (Array.isArray(ctx.body.files)) {
|
||||||
|
console.log("Found array of files");
|
||||||
|
for (const file of ctx.body.files) {
|
||||||
|
console.log(file);
|
||||||
|
await Bun.write(`${uploadsDir}${file.name}`, file);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
await Bun.write(`${uploadsDir}${ctx.body.files.name}`, ctx.body.files);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
.listen(3000);
|
.listen(3000);
|
||||||
|
|
||||||
console.log(
|
console.log(
|
||||||
`🦊 Elysia is running at http://${app.server?.hostname}:${app.server?.port}`
|
`🦊 Elysia is running at http://${app.server?.hostname}:${app.server?.port}`,
|
||||||
);
|
);
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@
|
||||||
<main class="container-fluid">
|
<main class="container-fluid">
|
||||||
|
|
||||||
<!-- File upload -->
|
<!-- File upload -->
|
||||||
<form method="post" enctype="multipart/form-data">
|
<form method="post" action="upload" enctype="multipart/form-data">
|
||||||
<div>
|
<div>
|
||||||
<article>
|
<article>
|
||||||
<table id="file-list">
|
<table id="file-list">
|
||||||
|
|
@ -48,7 +48,8 @@
|
||||||
</article>
|
</article>
|
||||||
</div>
|
</div>
|
||||||
<div class="center">
|
<div class="center">
|
||||||
<button type="submit">Convert</button>
|
<input type="submit" value="Convert">
|
||||||
|
<!-- <button type="submit">Convert</button> -->
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</main>
|
</main>
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
// Select the file input element
|
// Select the file input element
|
||||||
const fileInput = document.querySelector('input[type="file"]');
|
const fileInput = document.querySelector('input[type="file"]');
|
||||||
|
|
||||||
|
let filesToUpload = [];
|
||||||
|
|
||||||
// Add a 'change' event listener to the file input element
|
// Add a 'change' event listener to the file input element
|
||||||
fileInput.addEventListener("change", (e) => {
|
fileInput.addEventListener("change", (e) => {
|
||||||
console.log(e.target.files);
|
console.log(e.target.files);
|
||||||
|
|
@ -23,4 +25,24 @@ fileInput.addEventListener("change", (e) => {
|
||||||
// Append the row to the file-list table
|
// Append the row to the file-list table
|
||||||
fileList.appendChild(row);
|
fileList.appendChild(row);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
uploadFiles(files);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const uploadFiles = (files) => {
|
||||||
|
const formData = new FormData();
|
||||||
|
|
||||||
|
for (let i = 0; i < files.length; i++) {
|
||||||
|
formData.append("files", files[i], files[i].name);
|
||||||
|
}
|
||||||
|
|
||||||
|
fetch("/upload", {
|
||||||
|
method: "POST",
|
||||||
|
body: formData,
|
||||||
|
})
|
||||||
|
.then((res) => res.json())
|
||||||
|
.then((data) => {
|
||||||
|
console.log(data);
|
||||||
|
})
|
||||||
|
.catch((err) => console.log(err));
|
||||||
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue