mirror of
https://github.com/SillyTavern/SillyTavern.git
synced 2025-06-05 21:59:27 +02:00
Update upload to use fetch
This commit is contained in:
@@ -217,7 +217,7 @@ async function onCopyToSystemBackgroundClick(e) {
|
|||||||
const formData = new FormData();
|
const formData = new FormData();
|
||||||
formData.set('avatar', file);
|
formData.set('avatar', file);
|
||||||
|
|
||||||
uploadBackground(formData);
|
await uploadBackground(formData);
|
||||||
|
|
||||||
const list = chat_metadata[LIST_METADATA_KEY] || [];
|
const list = chat_metadata[LIST_METADATA_KEY] || [];
|
||||||
const index = list.indexOf(bgNames.oldBg);
|
const index = list.indexOf(bgNames.oldBg);
|
||||||
@@ -448,7 +448,7 @@ async function onBackgroundUploadSelected() {
|
|||||||
|
|
||||||
const formData = new FormData(form);
|
const formData = new FormData(form);
|
||||||
await convertFileIfVideo(formData);
|
await convertFileIfVideo(formData);
|
||||||
uploadBackground(formData);
|
await uploadBackground(formData);
|
||||||
form.reset();
|
form.reset();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -484,6 +484,7 @@ async function convertFileIfVideo(formData) {
|
|||||||
formData.set('avatar', convertedFile);
|
formData.set('avatar', convertedFile);
|
||||||
toastMessage.remove();
|
toastMessage.remove();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
formData.delete('avatar');
|
||||||
toastMessage.remove();
|
toastMessage.remove();
|
||||||
console.error('Error converting video to animated webp:', error);
|
console.error('Error converting video to animated webp:', error);
|
||||||
toastr.error('Error converting video to animated webp');
|
toastr.error('Error converting video to animated webp');
|
||||||
@@ -494,26 +495,34 @@ async function convertFileIfVideo(formData) {
|
|||||||
* Uploads a background to the server
|
* Uploads a background to the server
|
||||||
* @param {FormData} formData
|
* @param {FormData} formData
|
||||||
*/
|
*/
|
||||||
function uploadBackground(formData) {
|
async function uploadBackground(formData) {
|
||||||
jQuery.ajax({
|
try {
|
||||||
type: 'POST',
|
if (!formData.has('avatar')) {
|
||||||
url: '/api/backgrounds/upload',
|
console.log('No file provided. Background upload cancelled.');
|
||||||
data: formData,
|
return;
|
||||||
beforeSend: function () {
|
}
|
||||||
},
|
|
||||||
cache: false,
|
const headers = getRequestHeaders();
|
||||||
contentType: false,
|
delete headers['Content-Type'];
|
||||||
processData: false,
|
|
||||||
success: async function (bg) {
|
const response = await fetch('/api/backgrounds/upload', {
|
||||||
|
method: 'POST',
|
||||||
|
headers: getRequestHeaders(),
|
||||||
|
body: formData,
|
||||||
|
cache: 'no-cache',
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Failed to upload background');
|
||||||
|
}
|
||||||
|
|
||||||
|
const bg = await response.text();
|
||||||
setBackground(bg, generateUrlParameter(bg, false));
|
setBackground(bg, generateUrlParameter(bg, false));
|
||||||
await getBackgrounds();
|
await getBackgrounds();
|
||||||
highlightNewBackground(bg);
|
highlightNewBackground(bg);
|
||||||
},
|
} catch (error) {
|
||||||
error: function (jqXHR, exception) {
|
console.error('Error uploading background:', error);
|
||||||
console.log(exception);
|
}
|
||||||
console.log(jqXHR);
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user