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);
}
// Override fetch to include token (but NOT for FormData requests)
// Override fetch to include token
const originalFetch = window.fetch;
window.fetch = async function(resource, init = {}) {
// 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}`);
}
// Only set content type if not FormData (FormData sets its own)
if (!headers.has('Content-Type') && init.body && !(init.body instanceof FormData)) {
// Set content type if not set
if (!headers.has('Content-Type') && init.body) {
headers.set('Content-Type', 'application/json');
}
@@ -750,7 +750,7 @@ async function saveBanner(event) {
console.log(key, value);
}
// Create headers object - DO NOT set Content-Type for FormData!
// Create headers object
const headers = {};
// Add Authorization header if token exists
@@ -759,10 +759,12 @@ async function saveBanner(event) {
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', {
method: 'POST',
headers: headers, // No Content-Type header!
headers: headers,
body: formData
});
+11 -11
View File
@@ -156,20 +156,20 @@ func UpdateBannerHandler(w http.ResponseWriter, r *http.Request) {
// Log form values for debugging
log.Printf("Form values: %+v", r.Form)
// Parse style as JSON string
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
// Create a new banner with default values
newBanner := BannerContent{
Text: r.FormValue("text"),
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