Enviando um formulário com um anexo de arquivo
✅ Propósito do cenário:
- Recuperar a configuração do formulário do OneEntry CMS
- O usuário preenche um formulário (por exemplo, resposta, feedback).
- Anexa um arquivo (imagem, currículo, documento).
- Os dados e o arquivo são armazenados juntos em FormData
- Enviar os dados coletados para a API do OneEntry.
✅ O que você precisa:
- Uma PROJECT_URL e APP_TOKEN válidos para autenticação com a API do OneEntry.
- Um formulário no OneEntry com um campo do tipo "Arquivo" (tipo file)
- Marcador do formulário (por exemplo, resume_form)
📌 Importante:
- Não tratamos erros nesses exemplos.
- Você pode tratar erros usando try-catch ou em uma estrutura como "await Promise.catch((error) => error)"
Cenário
1. Importar oneEntry e definir URL e token
Exemplo:
import { defineOneEntry } from 'oneentry';
const PROJECT_URL = 'sua-url-do-projeto';
const APP_TOKEN = 'seu-token-do-app';
2. Criando um cliente API com a função defineOneEntry()
Exemplo:
const { Forms, FormData } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});
Exemplo:
const fileForm = await Forms.getFormByMarker('file');
Resultado:
{
"id": 8,
"attributeSetId": 11,
"type": "data",
"localizeInfos": {
"title": "Arquivo",
"titleForSite": "",
"successMessage": "Processamento de dados bem-sucedido",
"unsuccessMessage": "Processamento de dados malsucedido",
"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": "Arquivo"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
},
{
"type": "image",
"marker": "image",
"isLogin": null,
"isSignUp": null,
"position": 2,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {},
"localizeInfos": {
"title": "Imagem"
},
"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 Imagens"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
}
]
}
3. Enviando um arquivo com o formulário input=file
Enviar
Dados:
// JS
let selectedFiles = null;
// handleFileChange
const handleFileChange = (e: any) => {
selectedFiles = e.target.files;
};
// handleSubmit
const handleSubmit = async (e, file) => {
e.preventDefault();
// 4. Preparando os dados do formulário
const body = {
formIdentifier: 'file',
formData: [
{
marker: 'file',
type: 'file',
value: file,
fileQuery: {
type: 'page',
entity: 'editor',
id: 4965,
},
},
],
};
// 5. Enviando um formulário com FormData.postFormsData()
const postFormResp = await FormData.postFormsData(body);
};
Exemplo:
// 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": "Processamento de dados bem-sucedido"
}
Exemplo final
// 1. Importar oneEntry e definir PROJECT_URL e APP_TOKEN
import { defineOneEntry } from 'oneentry';
const PROJECT_URL = 'sua-url-do-projeto';
const APP_TOKEN = 'seu-token-do-app';
// 2. Criando um cliente API
const { Forms, FormData } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});
const fileForm = await Forms.getFormByMarker('file');
// 3. Enviando um arquivo com o formulário input=file
// JS
const selectedFiles = [];
const handleFileChange = (e) => {
selectedFiles = e.target.files;
};
// handleSubmit
const handleSubmit = async (e, file) => {
e.preventDefault();
// 4. Preparando os dados do formulário
const body = {
formIdentifier: 'file',
formData: [
{
marker: 'file',
type: 'file',
value: file,
fileQuery: {
type: 'page',
entity: 'editor',
id: 4965,
},
},
],
};
// 5. Enviando um formulário com 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>