Add files via upload

This commit is contained in:
Tomáš Dvořák
2025-05-27 08:34:00 +02:00
committed by GitHub
parent 41b3a47ab6
commit 7ad0f0fdcd
2 changed files with 515 additions and 513 deletions
+8 -6
View File
@@ -495,7 +495,7 @@ function showNotification(message, type = 'info') {
}, delay); }, delay);
} }
// Override fetch to include token (but NOT for FormData requests) // Override fetch to include token
const originalFetch = window.fetch; const originalFetch = window.fetch;
window.fetch = async function(resource, init = {}) { window.fetch = async function(resource, init = {}) {
// Add token to headers if it's an API request // Add token to headers if it's an API request
@@ -505,8 +505,8 @@ window.fetch = async function(resource, init = {}) {
headers.set('Authorization', `Bearer ${token}`); headers.set('Authorization', `Bearer ${token}`);
} }
// Only set content type if not FormData (FormData sets its own) // Set content type if not set
if (!headers.has('Content-Type') && init.body && !(init.body instanceof FormData)) { if (!headers.has('Content-Type') && init.body) {
headers.set('Content-Type', 'application/json'); headers.set('Content-Type', 'application/json');
} }
@@ -750,7 +750,7 @@ async function saveBanner(event) {
console.log(key, value); console.log(key, value);
} }
// Create headers object - DO NOT set Content-Type for FormData! // Create headers object
const headers = {}; const headers = {};
// Add Authorization header if token exists // Add Authorization header if token exists
@@ -759,10 +759,12 @@ async function saveBanner(event) {
headers['Authorization'] = `Bearer ${token}`; headers['Authorization'] = `Bearer ${token}`;
} }
// Send request with FormData (browser will set correct Content-Type with boundary) // Note: Don't set Content-Type header when using FormData with files
// The browser will set it automatically with the correct boundary
const response = await fetch('/api/banner/update', { const response = await fetch('/api/banner/update', {
method: 'POST', method: 'POST',
headers: headers, // No Content-Type header! headers: headers,
body: formData body: formData
}); });
+11 -11
View File
@@ -156,20 +156,20 @@ func UpdateBannerHandler(w http.ResponseWriter, r *http.Request) {
// Log form values for debugging // Log form values for debugging
log.Printf("Form values: %+v", r.Form) log.Printf("Form values: %+v", r.Form)
// Parse style as JSON string // Create a new banner with default values
styleJSON := r.FormValue("style")
var style BannerStyle
if err := json.Unmarshal([]byte(styleJSON), &style); err != nil {
log.Printf("Error parsing style JSON: %v", err)
http.Error(w, "Error parsing style data: "+err.Error(), http.StatusBadRequest)
return
}
// Create a new banner with parsed style
newBanner := BannerContent{ newBanner := BannerContent{
Text: r.FormValue("text"), Text: r.FormValue("text"),
Link: r.FormValue("link"), Link: r.FormValue("link"),
Style: style, Style: BannerStyle{
BackgroundColor: r.FormValue("style[backgroundColor]"),
TextColor: r.FormValue("style[textColor]"),
TextAlign: r.FormValue("style[textAlign]"),
FontSize: r.FormValue("style[fontSize]"),
Padding: r.FormValue("style[padding]"),
Margin: r.FormValue("style[margin]"),
BorderRadius: r.FormValue("style[borderRadius]"),
IsVisible: r.FormValue("style[isVisible]") == "true",
},
} }
// Log the banner data for debugging // Log the banner data for debugging