Prozentanzeige beim Bilder-Hochladen
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SvuGvHpbAQ4syfuLuRvutL
This commit is contained in:
parent
8c766093a9
commit
c50774440d
3 changed files with 45 additions and 7 deletions
13
src/app/admin/upload/route.ts
Normal file
13
src/app/admin/upload/route.ts
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
import { uploadImages } from "@/lib/upload";
|
||||||
|
|
||||||
|
// Eigener Endpunkt statt Server Action, damit der Browser den Upload-Fortschritt melden kann.
|
||||||
|
export async function POST(req: Request) {
|
||||||
|
let fd: FormData;
|
||||||
|
try {
|
||||||
|
fd = await req.formData();
|
||||||
|
} catch {
|
||||||
|
return Response.json({ urls: [], error: "Die Bilder konnten nicht gelesen werden." }, { status: 400 });
|
||||||
|
}
|
||||||
|
const res = await uploadImages(fd);
|
||||||
|
return Response.json(res, { status: res.error && res.urls.length === 0 ? 400 : 200 });
|
||||||
|
}
|
||||||
|
|
@ -1,23 +1,41 @@
|
||||||
"use client";
|
"use client";
|
||||||
/* eslint-disable @next/next/no-img-element */
|
/* eslint-disable @next/next/no-img-element */
|
||||||
import { useRef, useState } from "react";
|
import { useRef, useState } from "react";
|
||||||
import { uploadImages } from "@/lib/upload";
|
|
||||||
import { Icon } from "../Icon";
|
import { Icon } from "../Icon";
|
||||||
|
|
||||||
|
// XMLHttpRequest statt fetch, weil nur so der Upload-Fortschritt abrufbar ist.
|
||||||
|
function send(fd: FormData, onProgress: (p: number) => void): Promise<{ urls: string[]; error?: string }> {
|
||||||
|
return new Promise((resolve) => {
|
||||||
|
const xhr = new XMLHttpRequest();
|
||||||
|
xhr.open("POST", "/admin/upload");
|
||||||
|
xhr.responseType = "json";
|
||||||
|
xhr.upload.onprogress = (e) => {
|
||||||
|
if (e.lengthComputable) onProgress(Math.min(100, Math.round((e.loaded / e.total) * 100)));
|
||||||
|
};
|
||||||
|
xhr.onload = () => {
|
||||||
|
const body = xhr.response as { urls?: string[]; error?: string } | null;
|
||||||
|
if (body && Array.isArray(body.urls)) resolve({ urls: body.urls, error: body.error });
|
||||||
|
else resolve({ urls: [], error: xhr.status === 413 ? "Die Bilder sind zu groß." : "Hochladen fehlgeschlagen. Bitte versuche es erneut." });
|
||||||
|
};
|
||||||
|
xhr.onerror = () => resolve({ urls: [], error: "Hochladen fehlgeschlagen. Bitte prüfe deine Verbindung." });
|
||||||
|
xhr.send(fd);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export function ImageManager({ name, initial, multiple = true }: { name: string; initial: string[]; multiple?: boolean }) {
|
export function ImageManager({ name, initial, multiple = true }: { name: string; initial: string[]; multiple?: boolean }) {
|
||||||
const [images, setImages] = useState<string[]>(initial);
|
const [images, setImages] = useState<string[]>(initial);
|
||||||
const [busy, setBusy] = useState(false);
|
const [progress, setProgress] = useState<number | null>(null);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
const input = useRef<HTMLInputElement>(null);
|
const input = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
async function onFiles(files: FileList | null) {
|
async function onFiles(files: FileList | null) {
|
||||||
if (!files?.length) return;
|
if (!files?.length) return;
|
||||||
setBusy(true);
|
setProgress(0);
|
||||||
setError(null);
|
setError(null);
|
||||||
const fd = new FormData();
|
const fd = new FormData();
|
||||||
for (const f of Array.from(files)) fd.append("files", f);
|
for (const f of Array.from(files)) fd.append("files", f);
|
||||||
const res = await uploadImages(fd);
|
const res = await send(fd, setProgress);
|
||||||
setBusy(false);
|
setProgress(null);
|
||||||
if (res.error) setError(res.error);
|
if (res.error) setError(res.error);
|
||||||
setImages((cur) => (multiple ? [...cur, ...res.urls] : res.urls.slice(0, 1).length ? res.urls.slice(0, 1) : cur));
|
setImages((cur) => (multiple ? [...cur, ...res.urls] : res.urls.slice(0, 1).length ? res.urls.slice(0, 1) : cur));
|
||||||
if (input.current) input.current.value = "";
|
if (input.current) input.current.value = "";
|
||||||
|
|
@ -48,8 +66,13 @@ export function ImageManager({ name, initial, multiple = true }: { name: string;
|
||||||
{(multiple || images.length === 0) && (
|
{(multiple || images.length === 0) && (
|
||||||
<label className="ad-upload">
|
<label className="ad-upload">
|
||||||
<Icon name="image" />
|
<Icon name="image" />
|
||||||
{busy ? "Lädt hoch …" : multiple ? "Bilder hinzufügen" : "Bild wählen"}
|
{progress === null ? (multiple ? "Bilder hinzufügen" : "Bild wählen") : progress < 100 ? `Lädt hoch … ${progress} %` : "Wird gespeichert …"}
|
||||||
<input ref={input} type="file" accept="image/jpeg,image/png,image/webp,image/avif,image/gif" multiple={multiple} hidden onChange={(e) => onFiles(e.target.files)} />
|
{progress !== null && (
|
||||||
|
<span className="ad-upload__bar" role="progressbar" aria-valuemin={0} aria-valuemax={100} aria-valuenow={progress} aria-label="Upload-Fortschritt">
|
||||||
|
<span style={{ width: `${progress}%` }} />
|
||||||
|
</span>
|
||||||
|
)}
|
||||||
|
<input ref={input} disabled={progress !== null} type="file" accept="image/jpeg,image/png,image/webp,image/avif,image/gif" multiple={multiple} hidden onChange={(e) => onFiles(e.target.files)} />
|
||||||
</label>
|
</label>
|
||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -169,3 +169,5 @@ a.ad-kpi:hover { border-color: var(--line-strong); }
|
||||||
|
|
||||||
/* Felder nebeneinander: kein zusätzlicher Abstand oben, sonst sitzt das rechte Feld tiefer */
|
/* Felder nebeneinander: kein zusätzlicher Abstand oben, sonst sitzt das rechte Feld tiefer */
|
||||||
.ad-form .sm-form-row .sm-field + .sm-field { margin-top: 0; }
|
.ad-form .sm-form-row .sm-field + .sm-field { margin-top: 0; }
|
||||||
|
.ad-upload__bar { display: block; width: 80px; height: 4px; margin: var(--space-1) auto 0; border-radius: 2px; background: var(--line); overflow: hidden; }
|
||||||
|
.ad-upload__bar > span { display: block; height: 100%; background: var(--gold); transition: width 150ms linear; }
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue