Created
October 1, 2011 16:35
-
-
Save raffaeleguidi/1256286 to your computer and use it in GitHub Desktop.
A simple file upload and renderBinary example using Play! 1.2.3 and GAE (google appengine) 1.5.6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package controllers; | |
import play.*; | |
import play.data.Upload; | |
import play.db.Model.BinaryField; | |
import play.libs.MimeTypes; | |
import play.mvc.*; | |
import java.io.ByteArrayInputStream; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.util.List; | |
import com.google.appengine.api.datastore.Blob; | |
import com.google.apphosting.utils.remoteapi.RemoteApiPb.Response; | |
import models.*; | |
public class Application extends Controller { | |
public static void index() { | |
List images = Picture.all().fetch(); | |
render(images); | |
} | |
public static void uploadPicture(Upload data) { | |
Picture picture = new Picture(); | |
Logger.info(data.getContentType()); | |
Logger.info(data.getFieldName()); | |
Logger.info(data.getFileName()); | |
picture.contentType = data.getContentType(); | |
picture.fileName = data.getFileName(); | |
picture.file = data.asBytes(); | |
picture.save(); | |
Logger.info("saving id=%s", picture.id); | |
index(); | |
} | |
public static void show(Long id) { | |
Logger.info("loading id=%s", id); | |
Picture picture = Picture.findByKey(id); | |
response.setContentTypeIfNotSet(picture.contentType); | |
renderBinary(new ByteArrayInputStream(picture.file), picture.file.length); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#{extends 'main.html' /} | |
#{set title:'Home' /} | |
#{list items:images, as:'image'} | |
<a href="@{show(image.id)}"> | |
<img border="0" src="@{show(image.id)}" width="50px" /> | |
${image.fileName} | |
</a> | |
<br /> | |
#{/list} | |
<br /> | |
#{form @Application.uploadPicture(), enctype:'multipart/form-data'} | |
<input type="file" name="data" /> | |
<input type="submit" name="submit" value="Aggiungi" /> | |
#{/form} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package models; | |
import siena.*; | |
@Table("picture") | |
public class Picture extends Model { | |
@Id(Generator.AUTO_INCREMENT) | |
public Long id; | |
public byte[] file; | |
public String fileName; | |
public String contentType; | |
public static Query<Picture> all() { | |
return Model.all(Picture.class); | |
} | |
public static Picture findByKey(Long id) { | |
return all().getByKey(id); | |
} | |
} |
thanks, this was great!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Couldn't find a complete (working) example of html file upload and renderBinary using Play! framework on GAE and then spent some time to get it working. Here it is