Changes for page Development-Database vs Filesystem
Last modified by Pascal Robert on 2011/02/22 10:35
From version 3.1
edited by Quinton Dolan
on 2007/07/12 20:34
on 2007/07/12 20:34
Change comment:
There is no comment for this version
To version 1.1
edited by smmccraw
on 2007/07/08 09:45
on 2007/07/08 09:45
Change comment:
There is no comment for this version
Summary
-
Page properties (2 modified, 0 added, 0 removed)
Details
- Page properties
-
- Author
-
... ... @@ -1,1 +1,1 @@ 1 -XWiki. qdolan1 +XWiki.smmccraw - Content
-
... ... @@ -135,49 +135,53 @@ 135 135 136 136 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): 137 137 138 -{{ code}}138 +{{panel}} 139 139 140 -public WOActionResults imageAction() { 141 - // PictureTest is an EOEntity with a BLOB containing the image data 142 - PictureTest pt = getPictureTestEO(); 143 - return jpegResponseWithData(pt.image()); 144 -} 140 + public WOActionResults imageAction() 141 + { 142 + // PictureTest is an EOEntity with a BLOB containing the image data 143 + PictureTest pt = getPictureTestEO(); 144 + return jpegResponseWithData(pt.image()); 145 + } 146 + 147 + private PictureTest getPictureTestEO() 148 + { 149 + // Yes - you can get the session in a direct action 150 + // you just need to be prepared to deal with one not existing 151 + // whether you return an image if no session exists depends on 152 + // on your own application needs. 153 + WOSession theSession = existingSession(); 154 + EOEditingContext ec = (theSession == null) ? new EOEditingContext() : theSession.defaultEditingContext(); 155 + String picid = (String)request().formValueForKey("picid"); 156 + return (PictureTest)EOUtilities.objectMatchingKeyAndValue(ec, "PictureTest","id", new Integer(picid)); 157 + } 158 + 159 + private WOResponse jpegResponseWithData(NSData theData) 160 + { 161 + // This method returns the data so that the browser 162 + // recognizes the image type. In this particular application 163 + // I've just hardcoded a mime type of JPEG because I only 164 + // use JPEG images, but a better way would be to store the mime-type 165 + // that corresponds to the image data in the BLOB as a separate 166 + // field. I might revise this sample later on to show that. 167 + WOResponse response = WOApplication.application().createResponseInContext(context()); 168 + response.appendHeader("image/jpeg", "Content-Type"); 169 + response.appendContentData(theData); 170 + return response; 171 + } 145 145 146 -private PictureTest getPictureTestEO() { 147 - // Yes - you can get the session in a direct action 148 - // you just need to be prepared to deal with one not existing 149 - // whether you return an image if no session exists depends on 150 - // on your own application needs. 151 - WOSession theSession = existingSession(); 152 - EOEditingContext ec = (theSession == null) ? new EOEditingContext() : theSession.defaultEditingContext(); 153 - String picid = (String)request().formValueForKey("picid"); 154 - return (PictureTest)EOUtilities.objectMatchingKeyAndValue(ec, "PictureTest","id", new Integer(picid)); 155 -} 173 +{{/panel}} 156 156 157 -private WOResponse jpegResponseWithData(NSData theData) { 158 - // This method returns the data so that the browser 159 - // recognizes the image type. In this particular application 160 - // I've just hardcoded a mime type of JPEG because I only 161 - // use JPEG images, but a better way would be to store the mime-type 162 - // that corresponds to the image data in the BLOB as a separate 163 - // field. I might revise this sample later on to show that. 164 - WOResponse response = WOApplication.application().createResponseInContext(context()); 165 - response.appendHeader("image/jpeg", "Content-Type"); 166 - response.appendContentData(theData); 167 - return response; 168 -} 169 - 170 -{{/code}} 171 - 172 172 Then, in your WOComponent, you create a virtual accessor like this: 173 173 174 -{{ code}}177 +{{panel}} 175 175 176 -public String imageURL() { 177 - return context().directActionURLForActionNamed("icon", null) + "?picid=" + pictureItem.id(); 178 -} 179 + public String imageURL() 180 + { 181 + return context().directActionURLForActionNamed("icon", null) + "?picid=" + pictureItem.id(); 182 + } 179 179 180 -{{/ code}}184 +{{/panel}} 181 181 182 182 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. 183 183