AssetModel

View as Markdown

AssetModel

AssetModel holds the file and the details of an asset to upload. Pass it to the Create method to upload a new asset, or to the Update method to replace the file of an existing asset.

When to Use This Class: Use this class every time your code uploads a file to a stack, from a file path, a stream, or bytes in memory.

Conceptual Role: Acts as the request body of an upload. The SDK turns it into a multipart/form-data form through the IUploadInterface interface that AssetModel implements.

Returns: AssetModel. The file and details of an asset upload.

Constructors

AssetModel has four constructors. They differ only in the source of the file content. Each one takes the same optional details after contentType.

From a file path: AssetModel(string fileName, string filePath, string contentType, string? title = null, string? description = null, string? parentUID = null, string? tags = null)

From a stream: AssetModel(string fileName, Stream stream, string contentType, ...)

From bytes: AssetModel(string fileName, byte[] bytes, string contentType, ...)

From HTTP content: AssetModel(string fileName, ByteArrayContent byteArray, string contentType, ...)

NameTypeRequiredDefaultDescription
fileNamestringYesNot applicableNames the file in the upload form.
filePathstringYes (file path form)Not applicableReads the file at this path.
streamStreamYes (stream form)Not applicableReads the file from this stream.
bytesbyte[]Yes (bytes form)Not applicableUses these bytes as the file.
byteArrayByteArrayContentYes (HTTP content form)Not applicableUses this content as the file.
contentTypestringYesNot applicableSets the media type of the file, for example image/png.
titlestring?NonullSets the asset title.
descriptionstring?NonullSets the asset description.
parentUIDstring?NonullPlaces the asset in this folder.
tagsstring?NonullSets the asset tags.

Properties

NameTypeRequiredDefaultDescription
FileNamestringYesThe fileName constructor argumentFile name in the upload form. Sent as the file name of asset[upload].
ContentTypestringYesThe contentType constructor argumentMedia type of the file.
Titlestring?NoThe title constructor argumentAsset title. Sent as asset[title].
Descriptionstring?NoThe description constructor argumentAsset description. Sent as asset[description].
ParentUIDstring?NoThe parentUID constructor argumentUID of the folder that holds the asset. Sent as asset[parent_uid].
Tagsstring?NoThe tags constructor argumentAsset tags as one string. Sent as asset[tags] without changes.

Class-Level Notes

The constructor reads the file.

  • The file path form opens the file and reads it into memory when you create the model, not when the SDK sends the request. A later change to the file does not reach the upload.
  • The stream form reads stream.Length bytes from the current position of the stream. Set Position to 0 first if you read from the stream before.
  • The bytes form and the HTTP content form keep the content you pass.

Constructor checks.

  • Throws ArgumentNullException if fileName is null. An empty fileName passes the check.
  • Throws ArgumentNullException if bytes or byteArray is null. A null stream throws NullReferenceException.
  • .NET validates contentType when the SDK sets it on the file content. A null value throws ArgumentNullException, an empty value throws ArgumentException, and a value that is not a media type throws FormatException.
  • The file path form throws the .NET file exceptions, for example FileNotFoundException or DirectoryNotFoundException, when .NET cannot open the file.
  • The stream form throws EndOfStreamException when the stream holds fewer bytes after its current position than stream.Length, and NotSupportedException when the stream does not report a length.

Form fields.

  • The SDK sends asset[upload] with every request, and each of asset[title], asset[description], asset[parent_uid], and asset[tags] only when its property is not null.
  • The Content Management API (CMA) Upload asset request documents these form fields.

Method Index

Method NameReturnsDescription
GetHttpContentThe multipart form body of the upload.Builds the form that the SDK sends.

Class-Level Snippet

The snippet below builds a model from a file path with every detail set, and uploads it into a folder.

using System;
using Contentstack.Management.Core;
using Contentstack.Management.Core.Exceptions;
using Contentstack.Management.Core.Models;

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

AssetModel model = new AssetModel(
    "banner.png",
    "<FILE_PATH>",
    "image/png",
    title: "Home Banner",
    description: "Hero image for the home page",
    parentUID: "<FOLDER_UID>",
    tags: "banner");

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

Methods

Each method of the AssetModel class has its own section below, with its parameters, validation rules, behavior, and examples.

GetHttpContent

GetHttpContent builds the multipart/form-data form that the SDK sends when it uploads the model. The Create and Update methods of Asset call it for you.

This method takes no parameters.

Returns: HttpContent. The multipart form body of the upload.

Validation

  • Throws no exception of its own. The constructor already checked the file name and the file content.
  • GetHttpContent sends no request, so it raises no API error.

Behavior

  • Returns a new MultipartFormDataContent on each call.
  • Adds the file under asset[upload] with FileName as its file name.
  • Adds asset[title], asset[description], asset[parent_uid], and asset[tags] only for the properties that are not null.
  • Reads the properties when you call it, so a property you change after construction appears in the form. The file content stays the content the constructor received.

Example

Basic usage: inspect the form the SDK uploads

using System;
using System.Net.Http;
using System.Text;
using Contentstack.Management.Core.Models;

byte[] bytes = Encoding.UTF8.GetBytes("{\"campaign\": \"spring\"}");
AssetModel model = new AssetModel("campaign.json", bytes, "application/json", title: "Campaign Settings");

HttpContent content = model.GetHttpContent();
Console.WriteLine(content.Headers.ContentType);
Console.WriteLine(await content.ReadAsStringAsync());