Uploading files to the database or to the download folder, and then saving the link to the database?
I would like to save the link in the database and upload the image somewhere on your web server. Or, if you save the image in the database, save it as a thumb, this will keep your database size under control and your db size will be acceptable. Databases, in my opinion, are for data, not for assets such as images.
Files uploaded: http://www.playframework.org/documentation/2.0/JavaFileUpload
How I did it:
View
, enctype ( twitter)
@helper.form(controllers.orders.routes.Task.save, 'class -> "form-horizontal", 'enctype -> "multipart/form-data")
:
@inputFile(taskForm("file1"), '_display -> "Attachment", '_label -> Messages("file"))
MultipartFormData body = request().body().asMultipartFormData();
List<FilePart> resourceFiles = body.getFiles();
:
for (int i = 0; i < resourceFiles.size(); i++) {
FilePart picture = body.getFile(resourceFiles.get(i).getKey());
String fileName = picture.getFilename();
File file = picture.getFile();
File destinationFile = new File(play.Play.application().path().toString() + "//public//uploads//"
+ newTask.getCode() + "//" + i + "_" + fileName);
System.out.println(play.Play.application().path());
System.out.println(file.getAbsolutePath());
try {
FileUtils.copyFile(file, destinationFile);
TaskDocument taskDocument = new TaskDocument(newTask.description, "/assets/uploads/"
+ newTask.getCode() + "/" + i + "_" + fileName, loggedInUsr, newTask);
taskDocument.save();
} catch (IOException e) {
e.printStackTrace();
}
}
. :
: T000345
- 0_orange.png
- 1_apple.png
- 2_pear.png
EDIT: 2012-06-23
commons, Build.scala:
val appDependencies = Seq(
"mysql" % "mysql-connector-java" % "5.1.18",
"org.specs2" %% "specs2" % "1.9" % "test",
"commons-io" % "commons-io" % "2.2")