Get Started!

Attaching Assets

How to programmatically attach an asset to a DataItem.

Any DataItem can have any assets attached. You can use the built-in method CanvasCoreApp.ASSETS.AddAsset to record the relationship.

In addition to relating the DataItem and the Asset the method examines the file to extract mime, size, type etc.

It requires that the file is already uploaded to NetCanvas server See the asset upload module (somewhere else) for Uploading details or use asp:fileupload

The CanvasCoreApp.ASSETS.AddAsset method received an AddAsset object, that contains the details about the DataItem and Asset.

Annotated Example

Here's a verbose & annotated example. There's a lot of repetition, some normalization would be good, but this works :

// Reference some DataItem
var dataItem = MyApp.DB.DataItem.GetById(1);

// name, the file name
var name = "my-image.png";

// srcFile, the full path to the asset
var srcFile = Net4orceApp.cApplicationPath + "__tmp__/" + name

// Execute the Method
var result = CanvasCoreApp.ASSETS.AddAsset(

  // Passing AddAsset instance
  new AddAsset(  

    // Source File (absolute/full)
    srcFile,

    // Destination/Container Path (relative)
    @"__assets__",               


    // Destination File Path (relative)
    @"__assets__/" + name,  

    // Who's uploaded the Asset
    oSession.oUser,

    // Associate with DateItem Type
    dataItem.GetType().FullName,

    // And Associate with the specific DataItem
    dataItem.nId,

    // The Destination file name
    name,

    // Some Unique ref (used for uploader)
    Guid.NewGuid().ToString()
  )
);

Check out the AddAsset method for simplified method of associating Assets & DataItems.