java - How to upload Multiple files using play framework? -
i using play framework 2.1.2
using java , creating view upload multiple files , code here :
@form(action = routes.upload.up, 'enctype -> "multipart/form-data") { <input type="file" name="picture" accept="application/pdf" multiple="multiple"><br/> <input type="submit" value="upload"> }
i want upload doc , pdf file.
how restrict form upload doc , pdf file ?
i can java looking html code.
after want store multiple file permanent storage in computer.
and print name of file uploaded.
my code :
public static result up(){ multipartformdata md=request().body().asmultipartformdata(); list<filepart>file; file=md.getfiles(); for(filepart p: file){ logger.info(p.getfilename()); } return ok(file.get(0).getfilename()); }
it storing file temp directory want store permanent location not on temp directory file not temp file if upload a.docx
want store this file storage a.docx
name.
i don't want store file database.
and how list file uploaded file name?
i found question not getting answers because question old version.
give me idea fix issue.
here how implemented mine. apologize if made mistakes somewhere. "refactored" doesn't production code.
in html have:
<form name="fileuploadform" method="post" enctype="multipart/form-data" action="@routes.application.upload()"> file 1: <br /> <input type="file" name="filepart1" id="filepart1"><br /> file 2: <br /> <input type="file" name="filepart2" id="filepart1"><br /> </form>
in controller have:
public static result upload() { multipartformdata body = request().body().asmultipartformdata(); filepart filepart1 = body.getfile("filepart1"); filepart filepart2 = body.getfile("filepart2"); file newfile1 = new file("path in computer"); file newfile2 = new file("path in computer"); file file1 = filepart1.getfile(); file file2 = filepart2.getfile(); inputstream isfile1 = new fileinputstream(file1); inputstream isfile2 = new fileinputstream(file2); byte[] bytefile1 = ioutils.tobytearray(isfile1); byte[] bytefile2 = ioutils.tobytearray(isfile2); fileutils.writebytearraytofile(newfile1, bytefile1); fileutils.writebytearraytofile(newfile2, bytefile2); isfile1.close(); isfile2.close(); }
just kris said, have apache's commonio
you can buy adding build.scala found in /playproject/project:
import sbt._ import keys._ import play.project._ import com.typesafe.config._ object applicationbuild extends build { val appdependencies = seq( "commons-io" % "commons-io" % "2.4" //add here ) }
in implementation, can store files anywhere on computer specified in file newfile1
. have use database if want list files. have store file path string (varchar) in database. leave part figure out don't know how want handle file retrieval.
you can restrict user upload type of files using javascript. have javascript form validation checking file name: here example:
<script> var file1 = document.getelementbyid("filepart1").value; if (file1.indexof(".pdf") == -1) { alert("not pdf file!"); else { document.fileuploadform.submit(); } </script>
hope of helps.
Comments
Post a Comment