FileField

View as Markdown

FileField

FileField describes a file field in a content type or global field schema, with data_type set to file. It adds the allowed file extensions and size limits to the properties of Field.

When to Use This Class: Use this class for a field that holds any kind of asset, such as a PDF attachment. Use ImageField for a field that accepts only images.

Conceptual Role: Acts as the Field subclass that the SDK creates for a file field that has no dimension and no field_metadata.image set to true.

Returns: FileField. A file field of a schema.

Properties

FileField inherits every property of Field and adds the properties below.

NameTypeRequiredDefaultDescription
ExtensionsList<string>?NonullFile extensions the field accepts, for example pdf. Serialized as extensions.
Maxsizeint?NonullLargest file size the field accepts. Serialized as max.
MinSizeint?NonullSmallest file size the field accepts. Serialized as min.

Class-Level Notes

Property names.

  • Maxsize serializes as max and MinSize as min. The SDK leaves out each one when it is null.
  • ImageField derives from FileField, so an image field also has these properties.

Method Index

FileField has no methods. Set its properties and add it to ContentModelling.Schema.

Class-Level Snippet

The snippet below creates a content type with a file field that accepts PDF files.

using System;
using System.Collections.Generic;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;
using Contentstack.Management.Core.Models.Fields;

ContentstackClient client = new ContentstackClient("<AUTHTOKEN>");

ContentModelling model = new ContentModelling
{
    Title = "Blog Post",
    Uid = "blog_post",
    Schema = new List<Field>
    {
        new TextboxField { DisplayName = "Title", Uid = "title", DataType = "text", Mandatory = true, Unique = true },
        new FileField { DisplayName = "Attachment", Uid = "attachment", DataType = "file", Extensions = new List<string> { "pdf" } }
    }
};

try
{
    ContentstackResponse response = client.Stack("<API_KEY>").ContentType().Create(model);
    Console.WriteLine(response.OpenJsonObjectResponse()["notice"]);
}
catch (ContentstackErrorException e)
{
    // Thrown when the API rejects the request
    Console.WriteLine($"Request failed: {(int)e.StatusCode} {e.ErrorCode} {e.ErrorMessage}");
}