Re: Using a EO NSData Attribute in an HTML element's style
Re: Using a EO NSData Attribute in an HTML element's style
- Subject: Re: Using a EO NSData Attribute in an HTML element's style
- From: Lachlan Deck <email@hidden>
- Date: Sat, 14 Jun 2008 23:25:50 +1000
On 14/06/2008, at 6:42 AM, Mike Schrag wrote:
I'm storing my images as BLOBs in the database. I have a DIV whose
style attribute I want to include the background-image:url(value);
property with value being the location of the BLOB.
Is this possible? If so can someone shed some light for me?
I was hoping that I could use a WOGenericContainer with the style
attribute being set by a java string method.
There is a rather lengthy debate on this topic on the wiki (image in
the db vs the filesystem) which you should definitely check out if
you haven't already. Besides that, you would want to implement a
custom request handler that can vend out your images given a query
string/path ... Or just use ERAttachment framework (which does this).
Using ERAttachment would be preferable... but otherwise see [1]
with regards,
--
Lachlan Deck
[1] it's fairly trivial to have a direct action that provides binary
data (it doesn't matter if it's a pdf, gif, png etc).
I've got a BinaryInfo entity (which stores info about the binary data)
and then the BinaryData entity itself which just has the data. There's
only a relationship from BinaryData to BinaryInfo, and no inverse.
Ignore the fact that I'm using the shared editing context (it doesn't
matter for the example), but notice that the binary data is fetched
via raw rows.
e.g.,
BinaryImage : WOGenericElement {
elementName = "img";
height = binaryInfo.pixelHeight;
width = binaryInfo.pixelWidth;
alt = binaryInfo.name;
src = srcURL; // i.e.,
context().directActionURLForActionNamed( "binary", queryDictionary() );
}
public WOActionResults binaryAction()
{
try
{
// 1. find BinaryInfo via given 'id'. Will throw
EOObjectNotAvailable if not found
String id = request().stringFormValueForKey( "id" );
EOEntity binaryEntity =
EOModelGroup.defaultGroup().entityNamed( BinaryInfo.ENTITY_NAME );
String pkKey =
( String )binaryEntity.primaryKeyAttributeNames().objectAtIndex( 0 );
EOQualifier qualifier = binaryEntity.qualifierForPrimaryKey( new
NSDictionary( new Long( id ), pkKey ) );
BinaryInfo binaryInfo =
( BinaryInfo
)myApplication().sharedEntityRecord( BinaryInfo.ENTITY_NAME,
qualifier );
// 2. now create fetch spec for the binary data (via raw rows)
qualifier = new EOAndQualifier( new NSArray( new Object[] {
<...>
new EOKeyValueQualifier( BinaryData.BinaryInfoKey,
EOQualifier.QualifierOperatorEqual, binaryInfo )
} ) );
EOFetchSpecification fs = new
EOFetchSpecification( BinaryData.ENTITY_NAME, qualifier, null );
fs.setFetchesRawRows( true );
fs.setRefreshesRefetchedObjects( true );
fs.setFetchLimit( 1 );
// 3. fetch the raw row
EOSharedEditingContext sec =
EOSharedEditingContext.defaultSharedEditingContext();
sec.lockForReading();
try
{
NSArray results = sec.objectsWithFetchSpecification( fs );
if ( results != null && results.count() == 1 )
{
NSDictionary binaryRecord =
( NSDictionary )results.objectAtIndex( 0 );
Object theBytes = binaryRecord.valueForKey( BinaryData.ContentKey );
if ( theBytes == null )
theBytes = NSData.EmptyData;
return binaryResponseWithData( ( NSData )theBytes,
binaryInfo.mimeType(), binaryInfo.name() );
}
}
finally
{
sec.unlockForReading();
}
}
catch ( EOObjectNotAvailableException e )
{
LOG.debug( "No object found", e );
}
catch ( NumberFormatException e )
{
LOG.debug( "Invalid binary id", e );
}
return binaryResponseWithData( NSData.EmptyData, "image/gif",
"dummy.gif" );
}
private WOResponse binaryResponseWithData( NSData theData, String
contentType, String fileName )
{
// This method returns the data so that the browser
// recognizes the mime type.
WOResponse response =
myApplication().createResponseInContext( context() );
if ( contentType.startsWith( "image" ) )
{
response.appendHeader( contentType, "Content-Type" );
}
else
{
response.appendHeader( contentType + "; name=\"" + fileName + "\"",
"Content-Type" );
response.appendHeader( fileName, "Content-Description" );
response.appendHeader( "inline; filename=\"" + fileName + "\"",
"Content-Disposition" );
}
response.appendHeader( String.valueOf( theData.length() ), "Content-
Length" );
response.appendContentData( theData );
return response;
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden