Saltar al contenido principal

Enviando un formulario con un archivo adjunto

✅ Propósito del escenario:

  • Recuperar la configuración del formulario desde OneEntry CMS
  • El usuario completa un formulario (por ejemplo, respuesta, comentarios).
  • Adjunta un archivo (imagen, currículum, documento).
  • Los datos y el archivo se almacenan juntos en FormData
  • Enviar los datos recopilados a la API de OneEntry.

✅ Lo que necesitas:

  • Una PROJECT_URL y APP_TOKEN válidos para la autenticación con la API de OneEntry.
  • Un formulario en OneEntry con un campo de tipo "Archivo" (tipo file)
  • Marcador del formulario (por ejemplo, resume_form)

📌 Importante:

  • No manejamos errores en estos ejemplos.
  • Puedes manejar errores usando try-catch o en una estructura como "await Promise.catch((error) => error)"

Escenario

1. Importar oneEntry y definir URL y token

Ejemplo:

import { defineOneEntry } from 'oneentry';

const PROJECT_URL = 'tu-url-del-proyecto';
const APP_TOKEN = 'tu-token-de-app';

2. Crear un cliente API con la función defineOneEntry()

Ejemplo:

const { Forms, FormData } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

Ejemplo:

const fileForm = await Forms.getFormByMarker('file');
Resultado:
{
"id": 8,
"attributeSetId": 11,
"type": "data",
"localizeInfos": {
"title": "Archivo",
"titleForSite": "",
"successMessage": "Procesamiento de datos exitoso",
"unsuccessMessage": "Procesamiento de datos fallido",
"urlAddress": "",
"database": "0",
"script": "0"
},
"version": 0,
"position": 1,
"identifier": "file",
"processingType": "script",
"templateId": null,
"attributes": [
{
"type": "file",
"marker": "file",
"isLogin": null,
"isSignUp": null,
"position": 1,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {},
"localizeInfos": {
"title": "Archivo"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
},
{
"type": "image",
"marker": "image",
"isLogin": null,
"isSignUp": null,
"position": 2,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {},
"localizeInfos": {
"title": "Imagen"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
},
{
"type": "groupOfImages",
"marker": "images_group",
"isLogin": null,
"isSignUp": null,
"position": 3,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {},
"localizeInfos": {
"title": "Grupo de imágenes"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
}
]
}

3. Subir un archivo con la entrada del formulario input=file

Enviar

Datos:

// JS
let selectedFiles = null;

// handleFileChange
const handleFileChange = (e: any) => {
selectedFiles = e.target.files;
};

// handleSubmit
const handleSubmit = async (e, file) => {
e.preventDefault();

// 4. Preparando los datos del formulario
const body = {
formIdentifier: 'file',
formData: [
{
marker: 'file',
type: 'file',
value: file,
fileQuery: {
type: 'page',
entity: 'editor',
id: 4965,
},
},
],
};

// 5. Enviando un formulario con FormData.postFormsData()
const postFormResp = await FormData.postFormsData(body);
};

Ejemplo:

// HTML
<form
onSubmit={(e) => handleSubmit(e, selectedFiles)}
className="flex flex-col gap-3 p-10 border rounded-3xl"
>
<input
type="file"
onChange={handleFileChange}
className="bg-white text-slate-800 p-3 rounded-2xl"
/>
<button
type="submit"
className="bg-white text-slate-800 p-3 rounded-2xl"
>
Enviar
</button>
</form>
Resultado:
{
"formData": {
"formIdentifier": "file",
"time": "2025-05-12T22:28:57.531Z",
"formData": [
{
"marker": "file",
"type": "file",
"value": [
{
"filename": "files/project/page/4965/editor/process-svgrepo-com-1747088937134.svg",
"downloadLink": "https://test-data.oneentry.cloud/cloud-static/files/project/page/4965/editor/process-svgrepo-com-1747088937134.svg",
"size": 67066
}
]
}
],
"id": 76
},
"actionMessage": "Procesamiento de datos exitoso"
}

Ejemplo final

// 1. Importar oneEntry y definir PROJECT_URL y APP_TOKEN
import { defineOneEntry } from 'oneentry';

const PROJECT_URL = 'tu-url-del-proyecto';
const APP_TOKEN = 'tu-token-de-app';

// 2. Crear un cliente API
const { Forms, FormData } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

const fileForm = await Forms.getFormByMarker('file');

// 3. Subir un archivo con la entrada del formulario input=file
// JS
const selectedFiles = [];
const handleFileChange = (e) => {
selectedFiles = e.target.files;
};

// handleSubmit
const handleSubmit = async (e, file) => {
e.preventDefault();

// 4. Preparando los datos del formulario
const body = {
formIdentifier: 'file',
formData: [
{
marker: 'file',
type: 'file',
value: file,
fileQuery: {
type: 'page',
entity: 'editor',
id: 4965,
},
},
],
};

// 5. Enviando un formulario con FormData.postFormsData()
const postFormResp = await FormData.postFormsData(body);
};

// HTML
<form
onSubmit={(e) => handleSubmit(e, selectedFiles)}
className="flex flex-col gap-3 p-10 border rounded-3xl"
>
<input
type="file"
onChange={handleFileChange}
className="bg-white text-slate-800 p-3 rounded-2xl"
/>
<button
type="submit"
className="bg-white text-slate-800 p-3 rounded-2xl"
>
Enviar
</button>
</form>