Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X
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.data.UploadedFile
import com.axeda.drm.sdk.data.UploadedFileFinder
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(parameters.id 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) {
logger.info(e.message);
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 = is.read(byteBuffer)) != -1) {
// appends buffer
answer.write(byteBuffer, 0, nbByteRead);
}
} finally {
is.close()
}
return answer.toByteArray();
}