#40 fix: upload de imágenes con multipart/form-data

This commit is contained in:
mono
2026-06-13 17:59:14 -05:00
parent a2ab4fceb7
commit 5397ab2255
2 changed files with 23 additions and 10 deletions

View File

@@ -100,13 +100,14 @@
/> />
<v-file-input <v-file-input
v-model="form.file" ref="fileInputRef"
label="Imagen" label="Imagen"
accept="image/*" accept="image/*"
:rules="[(v) => !!v || 'Seleccione una imagen']" :rules="[(v) => !!v || 'Seleccione una imagen']"
variant="outlined" variant="outlined"
prepend-icon="mdi-camera" prepend-icon="mdi-camera"
class="mb-3" class="mb-3"
@update:model-value="onFileSelected"
/> />
<v-img <v-img
@@ -200,9 +201,11 @@ const dialog = ref({
editingItem: null, editingItem: null,
}); });
const fileInputRef = ref(null);
const selectedFile = ref(null);
const form = ref({ const form = ref({
product: null, product: null,
file: null,
preview: null, preview: null,
}); });
@@ -229,7 +232,7 @@ const productOptions = computed(() => {
}); });
watch( watch(
() => form.value.file, selectedFile,
(file) => { (file) => {
if (previewObjectUrl) { if (previewObjectUrl) {
URL.revokeObjectURL(previewObjectUrl); URL.revokeObjectURL(previewObjectUrl);
@@ -246,6 +249,10 @@ watch(
}, },
); );
function onFileSelected(file) {
selectedFile.value = file;
}
function formatDate(dateStr) { function formatDate(dateStr) {
if (!dateStr) return "-"; if (!dateStr) return "-";
const d = new Date(dateStr); const d = new Date(dateStr);
@@ -276,14 +283,15 @@ async function loadProducts() {
function openCreateDialog() { function openCreateDialog() {
dialog.value = { show: true, isEdit: false, editingItem: null }; dialog.value = { show: true, isEdit: false, editingItem: null };
form.value = { product: null, file: null, preview: null }; selectedFile.value = null;
form.value = { product: null, preview: null };
} }
function openEditDialog(item) { function openEditDialog(item) {
dialog.value = { show: true, isEdit: true, editingItem: item }; dialog.value = { show: true, isEdit: true, editingItem: item };
selectedFile.value = null;
form.value = { form.value = {
product: item.product, product: item.product,
file: null,
preview: item.image, preview: item.image,
}; };
} }
@@ -293,10 +301,11 @@ function closeDialog() {
URL.revokeObjectURL(previewObjectUrl); URL.revokeObjectURL(previewObjectUrl);
previewObjectUrl = null; previewObjectUrl = null;
} }
selectedFile.value = null;
dialog.value.show = false; dialog.value.show = false;
dialog.value.isEdit = false; dialog.value.isEdit = false;
dialog.value.editingItem = null; dialog.value.editingItem = null;
form.value = { product: null, file: null, preview: null }; form.value = { product: null, preview: null };
} }
async function submitForm() { async function submitForm() {
@@ -310,8 +319,8 @@ async function submitForm() {
try { try {
const fd = new FormData(); const fd = new FormData();
fd.append("product", form.value.product); fd.append("product", form.value.product);
if (form.value.file) { if (selectedFile.value) {
fd.append("image", form.value.file); fd.append("image", selectedFile.value);
} }
if (dialog.value.isEdit) { if (dialog.value.isEdit) {

View File

@@ -138,12 +138,16 @@ class DjangoApi {
createCatalogueImage(data) { createCatalogueImage(data) {
const url = this.base + "/don_confiao/api/catalogue_images/"; const url = this.base + "/don_confiao/api/catalogue_images/";
return http.post(url, data).then((r) => r.data); return http.post(url, data, {
headers: { 'Content-Type': undefined },
}).then((r) => r.data);
} }
updateCatalogueImage(id, data) { updateCatalogueImage(id, data) {
const url = this.base + `/don_confiao/api/catalogue_images/${id}/`; const url = this.base + `/don_confiao/api/catalogue_images/${id}/`;
return http.put(url, data).then((r) => r.data); return http.put(url, data, {
headers: { 'Content-Type': undefined },
}).then((r) => r.data);
} }
deleteCatalogueImage(id) { deleteCatalogueImage(id) {