> ## Documentation Index
> Fetch the complete documentation index at: https://help.teable.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# رفع مرفق

> رفع ملفات محلية أو ملفات عبر عنوان URL إلى نهاية حقل مرفقات في سجل محدد

### المسار

POST /api/table/\{tableId}/record/\{recordId}/\{fieldId}/uploadAttachment

### الطلب

#### معلمات المسار

* tableId (string): المعرّف الفريد للجدول [(كيفية الحصول عليه)](/ar/api-doc/get-id#tableid).
* recordId (string): المعرّف الفريد للسجل المطلوب تحديثه [(كيفية الحصول عليه)](/ar/api-doc/get-id#recordid).
* fieldId (string): معرّف حقل المرفقات المطلوب الرفع إليه [(كيفية الحصول عليه)](/ar/api-doc/get-id#fieldid)

يمكن أن تحتوي حقول المرفقات على عدة مرفقات. تتيح واجهة API هذه رفع مرفق واحد في كل مرة إلى نهاية الخلية.

لحذف المرفقات أو إعادة ترتيبها، استخدم [واجهة API لتحديث السجل](/ar/api-doc/record/update).

يجب أن يشير fieldId إلى حقل من نوع المرفقات.

يقتصر حجم المرفقات المرفوعة عبر API على 100MB في الإصدار السحابي، ولا يوجد حد في الإصدار ذي الاستضافة الذاتية.

#### نص الطلب

النوع: formData

المعلمات:

* **file (اختياري)**
  * الوصف: بيانات السجل المطلوب تحديثها
  * النوع: Buffer أو ReadStream
* **fileUrl (اختياري)**
  * الوصف: عنوان URL الذي سيتم الرفع منه
  * النوع: سلسلة نصية
  * مثال: `https://example.com/image.jpg`
  * ملاحظة: يمكن تحديد أحد الخيارين file أو fileUrl فقط في كل مرة. إذا حُددا معًا، فتكون الأولوية لـ file.

### الاستجابة

#### استجابة النجاح

* رمز الحالة: 201 Created
* نص الاستجابة: يُرجع بيانات السجل المحدّثة.

**مثال على نص الاستجابة**

```json theme={null}
{
    "id": "rec123456789ABCDE",
    "fields": {
      "fld123456789ABCDE": [
        {
          "id": "act75TiSyhcS7hfrizW",
          "name": "example.jpg",
          "path": "table/example",
          "size": 392903,
          "token": "tokenxxxxx",
          "width": 976,
          "height": 1000,
          "mimetype": "image/jpeg",
          "presignedUrl": "https://app.teable.ai/preview/previewURL"
        }
      ],
    }
}
```

#### استجابات الأخطاء

* رمز الحالة: 400 Bad Request: خطأ في تنسيق نص الطلب أو غياب الحقول المطلوبة.
* رمز الحالة: 404 Not Found: قيمة tableId أو recordId المحددة غير موجودة.

### مثال على الشيفرة

<CodeGroup>
  ```bash CURL theme={null}
  # الرفع عبر ملف

  curl -X POST 'https://app.teable.ai/api/table/__tableId__/record/__recordId__/__fieldId__/uploadAttachment' \
    -H 'Authorization: Bearer __token__' \
    -H 'Content-Type: multipart/form-data' \
    -F 'file=@/path/to/your/file.jpg'

  # الرفع عبر عنوان URL
  curl -X POST 'https://app.teable.ai/api/table/__tableId__/record/__recordId__/__fieldId__/uploadAttachment' \
    -H 'Authorization: Bearer __token__' \
    -H 'Content-Type: multipart/form-data' \
    -F 'fileUrl=https://example.com/image.jpg'
  ```

  ```js JS SDK theme={null}
  import { configApi, uploadAttachment } from '@teable/openapi';

  configApi({
    endpoint: 'https://app.teable.ai',
    token: '__token__',
  });

  // بيئة Node.js: رفع ملف محلي
  const fileStream = fs.createReadStream('/path/to/your/file.jpg');
  const response = await uploadAttachment('__tableId__', '__recordId__', '__fieldId__', fileStream);

  console.log(response.data);

  // رفع عنوان URL (يعمل في بيئتي Node.js والمتصفح)
  const response = await uploadAttachment('__tableId__', '__recordId__', '__fieldId__', 'https://example.com/image.jpg');

  console.log(response.data);

  // بيئة المتصفح: رفع ملف
  // بافتراض وجود عنصر لإدخال الملف: <input type="file" id="fileInput">
  document.getElementById('fileInput').addEventListener('change', async (event) => {
    const file = event.target.files[0];
    if (file) {
      const response = await uploadAttachment('__tableId__', '__recordId__', '__fieldId__', file);
      console.log(response.data);
    }
  });
  ```

  ```ts TypeScript theme={null}
  import FormData from 'form-data';
  import fs from 'fs';

  // بيئة Node.js: رفع ملف محلي
  const formData = new FormData();
  formData.append('file', fs.createReadStream('/path/to/your/file.jpg'));

  const response = await fetch('https://app.teable.ai/api/table/__tableId__/record/__recordId__/__fieldId__/uploadAttachment', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer __token__',
      ...formData.getHeaders()
    },
    body: formData
  });

  console.log(await response.json());

  // رفع عنوان URL (يعمل في بيئتي Node.js والمتصفح)
  const formDataUrl = new FormData();
  formDataUrl.append('fileUrl', 'https://example.com/image.jpg');

  const responseUrl = await fetch('https://app.teable.ai/api/table/__tableId__/record/__recordId__/__fieldId__/uploadAttachment', {
    method: 'POST',
    headers: {
      'Authorization': 'Bearer __token__',
      ...formDataUrl.getHeaders()
    },
    body: formDataUrl
  });

  console.log(await responseUrl.json());

  // بيئة المتصفح: رفع ملف
  // بافتراض وجود عنصر لإدخال الملف: <input type="file" id="fileInput">
  document.getElementById('fileInput').addEventListener('change', async (event: Event) => {
    const fileInput = event.target as HTMLInputElement;
    const file = fileInput.files?.[0];
    if (file) {
      const formData = new FormData();
      formData.append('file', file);

      const response = await fetch('https://app.teable.ai/api/table/__tableId__/record/__recordId__/__fieldId__/uploadAttachment', {
        method: 'POST',
        headers: {
          'Authorization': 'Bearer __token__'
        },
        body: formData
      });

      console.log(await response.json());
    }
  });
  ```

  ```python Python theme={null}
  import requests
  import mimetypes
  import os

  # رفع ملف محلي
  file_path = '/path/to/your/file.jpg'
  with open(file_path, 'rb') as file:
      file_name = os.path.basename(file_path)
      mime_type, _ = mimetypes.guess_type(file_path)
      files = {'file': (file_name, file, mime_type)}
      response = requests.post(
          'https://app.teable.ai/api/table/__tableId__/record/__recordId__/__fieldId__/uploadAttachment',
          headers={
              'Authorization': 'Bearer __token__'
          },
          files=files
      )

  print(response.json())

  # رفع عنوان URL
  response_url = requests.post(
      'https://app.teable.ai/api/table/__tableId__/record/__recordId__/__fieldId__/uploadAttachment',
      headers={
          'Authorization': 'Bearer __token__'
      },
      data={'fileUrl': 'https://example.com/image.jpg'}
  )

  print(response_url.json())
  ```
</CodeGroup>
