...
Generally, I create a direct action method for dispensing the images. Something like this in your DirectAction (except that you might want to add validation if you don't want just anyone getting to your images by hacking the URL):
Panel |
---|
Wiki Markup |
Code Block |
public WOActionResults imageAction() {
// PictureTest is an EOEntity with a BLOB containing the image data
PictureTest pt = getPictureTestEO();
return jpegResponseWithData(pt.image());
}
private PictureTest getPictureTestEO()
{
// Yes - you can get the session in a direct action
// you just need to be prepared to deal with one not existing
// whether you return an image if no session exists depends on
// on your own application needs.
WOSession theSession = existingSession();
EOEditingContext ec = (theSession == null) ? new EOEditingContext() : theSession.defaultEditingContext();
String picid = (String)request().formValueForKey("picid");
return (PictureTest)EOUtilities.objectMatchingKeyAndValue(ec, "PictureTest","id", new Integer(picid));
}
private WOResponse jpegResponseWithData(NSData theData)
{
// This method returns the data so that the browser
// recognizes the image type. In this particular application
// I've just hardcoded a mime type of JPEG because I only
// use JPEG images, but a better way would be to store the mime-type
// that corresponds to the image data in the BLOB as a separate
// field. I might revise this sample later on to show that.
WOResponse response = WOApplication.application().createResponseInContext(context());
response.appendHeader("image/jpeg", "Content-Type");
response.appendContentData(theData);
return response;
}
|
Then, in your WOComponent, you create a virtual accessor like this:
Panel |
---|
Code Block |
public String imageURL()
{
return context().directActionURLForActionNamed("icon", null) + "?picid=" + pictureItem.id();
}
|
in this case, pictureItem is a PictureItem EOEntity instance that I use in a WORepetition - I'm just pulling the id number from the currently selected picture.
...