| 494 | 495 | * Uploads a background to the server |
| 495 | 496 | * @param {FormData} formData |
| 496 | 497 | */ |
| 497 | 498 | async function uploadBackground(formData) { |
| 498 | | - jQuery.ajax({ |
| 499 | + try { |
| 499 | | - type: 'POST', |
| 500 | + if (!formData.has('avatar')) { |
| 500 | | - url: '/api/backgrounds/upload', |
| 501 | + console.log('No file provided. Background upload cancelled.'); |
| 501 | | - data: formData, |
| 502 | + return; |
| 502 | | - beforeSend: function () { |
| 503 | + } |
| 503 | | - }, |
| 504 | + |
| 504 | | - cache: false, |
| 505 | + const headers = getRequestHeaders(); |
| 505 | | - contentType: false, |
| 506 | + delete headers['Content-Type']; |
| 506 | | - processData: false, |
| 507 | + |
| 507 | | - success: async function (bg) { |
| 508 | + const response = await fetch('/api/backgrounds/upload', { |
| 509 | + method: 'POST', |
| 510 | + headers: getRequestHeaders(), |
| 511 | + body: formData, |
| 512 | + cache: 'no-cache', |
| 513 | + }); |
| 514 | + |
| 515 | + if (!response.ok) { |
| 516 | + throw new Error('Failed to upload background'); |
| 517 | + } |
| 518 | + |
| 519 | + const bg = await response.text(); |
| 508 | 520 | setBackground(bg, generateUrlParameter(bg, false)); |
| 509 | 521 | await getBackgrounds(); |
| 510 | 522 | highlightNewBackground(bg); |
| 511 | | - }, |
| 523 | + } catch (error) { |
| 512 | | - error: function (jqXHR, exception) { |
| 524 | + console.error('Error uploading background:', error); |
| 513 | | - console.log(exception); |
| 525 | + } |
| 514 | | - console.log(jqXHR); |
| 515 | | - }, |
| 516 | | - }); |
| 517 | 526 | } |
| 518 | 527 | |
| 519 | 528 | /** |