Ana içeriğe geç

Bir dosya ekli form gönderme

✅ Senaryonun amacı:

  • OneEntry CMS'den form yapılandırmasını almak
  • Kullanıcı bir formu doldurur (örneğin, yanıt, geri bildirim).
  • Bir dosya ekler (görüntü, özgeçmiş, belge).
  • Veriler ve dosya FormData içinde birlikte saklanır.
  • Toplanan verileri OneEntry API'sine gönderin.

✅ İhtiyacınız olanlar:

  • OneEntry API'si ile kimlik doğrulama için geçerli bir PROJECT_URL ve APP_TOKEN.
  • OneEntry'de "Dosya" (type file) türünde bir alan içeren bir form.
  • Form işaretleyici (örneğin, resume_form)

📌 Önemli:

  • Bu örneklerde hataları ele almıyoruz.
  • Hataları try-catch kullanarak veya "await Promise.catch((error) => error)" gibi bir yapı içinde ele alabilirsiniz.

Senaryo

1. oneEntry'yi içe aktarın ve URL ile token'ı tanımlayın

Örnek:

import { defineOneEntry } from 'oneentry';

const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';

2. defineOneEntry() fonksiyonu ile bir API istemcisi oluşturma

Örnek:

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

Örnek:

const fileForm = await Forms.getFormByMarker('file');
Sonuç:
{
"id": 8,
"attributeSetId": 11,
"type": "data",
"localizeInfos": {
"title": "Dosya",
"titleForSite": "",
"successMessage": "Veri işleme başarılı",
"unsuccessMessage": "Veri işleme başarısız",
"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": "Dosya"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
},
{
"type": "image",
"marker": "image",
"isLogin": null,
"isSignUp": null,
"position": 2,
"settings": {},
"isVisible": true,
"listTitles": [],
"validators": {},
"localizeInfos": {
"title": "Görüntü"
},
"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": "Görüntü grubu"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
}
]
}

3. form input=file ile bir dosya yükleme

Gönder

Veri:

// JS
let selectedFiles = null;

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

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

// 4. Form verilerini hazırlama
const body = {
formIdentifier: 'file',
formData: [
{
marker: 'file',
type: 'file',
value: file,
fileQuery: {
type: 'page',
entity: 'editor',
id: 4965,
},
},
],
};

// 5. FormData.postFormsData() ile bir form gönderme
const postFormResp = await FormData.postFormsData(body);
};

Örnek:

// 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"
>
Gönder
</button>
</form>
Sonuç:
{
"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": "Veri işleme başarılı"
}

Son örnek

// 1. oneEntry'yi içe aktarın ve PROJECT_URL ile APP_TOKEN'ı tanımlayın
import { defineOneEntry } from 'oneentry';

const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';

// 2. Bir API istemcisi oluşturma
const { Forms, FormData } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});

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

// 3. form input=file ile bir dosya yükleme
// JS
const selectedFiles = [];
const handleFileChange = (e) => {
selectedFiles = e.target.files;
};

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

// 4. Form verilerini hazırlama
const body = {
formIdentifier: 'file',
formData: [
{
marker: 'file',
type: 'file',
value: file,
fileQuery: {
type: 'page',
entity: 'editor',
id: 4965,
},
},
],
};

// 5. FormData.postFormsData() ile bir form gönderme
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"
>
Gönder
</button>
</form>