Showing results for 
Search instead for 
Did you mean: 
Showing results for 
Search instead for 
Did you mean: 

Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X

Scripto Example: Retrieve an asset-uploaded file

No ratings

This example shows how a file can be retrieved via Scripto and then displayed on a Web page. Precondition is that an asset has an uploaded file. This script assumes the file is there and that it is not extremely large (under 1 megabyte). This example uses base64 encoding to convert the file into a string. Future versions of Scripto will support other data streams so that base64 encoding will not be necessary.

import com.axeda.drm.sdk.Context



import com.axeda.drm.sdk.device.Device

import com.axeda.drm.sdk.device.DeviceFinder

// This script requires parameter "id"

Context ctx = Context.create(parameters.username);

def response = ''

try {

    DeviceFinder deviceFinder = new DeviceFinder(ctx, new Identifier( as Integer));

    Device device = deviceFinder.find();

    UploadedFileFinder uff = new UploadedFileFinder(ctx)

    uff.device = device

    uff.hint = 'photo'

    def ufiles = uff.findAll()

    UploadedFile ufile

    if (ufiles.size() > 0) {

        ufile = ufiles[0]

        File f = ufile.extractFile()

        response = getBytes(f).encodeBase64(false).toString()



catch (Exception e) {;

    response = [

            faultcode: 'Groovy Exception',

            faultstring: e.message



return ['Content-Type': 'data:image/png;base64', 'Content': response];

static byte[] getBytes(File file) throws IOException {

    return getBytes(new FileInputStream(file));


static byte[] getBytes(InputStream is) throws IOException {

    ByteArrayOutputStream answer = new ByteArrayOutputStream();

// reading the content of the file within a byte buffer

    byte[] byteBuffer = new byte[8192];

    int nbByteRead /* = 0*/;

    try {

        while ((nbByteRead = != -1) {

// appends buffer

            answer.write(byteBuffer, 0, nbByteRead);


    } finally {



    return answer.toByteArray();


Version history
Last update:
‎Jun 01, 2016 11:08 AM
Updated by:
Labels (2)