如何使用播放框架上传多个文件? [英] How to upload Multiple files using play framework?

查看:135
本文介绍了如何使用播放框架上传多个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 play framework 2.1.2 使用java ,我正在创建视图来上传多个文件,我的代码在这里: >

  @form(action = routes.upload.up,'enctype  - >multipart / form-data){
< input type =filename =pictureaccept =application / pdfmultiple =multiple>< br />
< input type =submitvalue =upload>




我只想上传doc和pdf文件即可。

如何限制表单只上传doc和pdf文件?

我可以这与java,但我正在寻找html代码。

之后,我想存储多个文件到永久存储在我的电脑。



和我上传的文件的名称



我的代码:

  public static result(){


MultipartFormData md = request()。body()。asMultipartFormData();

列出< FilePart>档案;

file = md.getFiles(); (FilePart p:file){
Logger.info(p.getFilename());


}

return ok(file.get(0).getFilename());

将文件存入临时目录要将存储到永久位置而不是临时目录作为文件而不是临时文件就好像我上传 a.docx 我要存储这个文件到存储与 a.docx 名称



我不想将文件存储到数据库中。

如何列出我通过文件名上传的所有文件? b
$ b

我发现了一些问题,但我没有得到答案,因为这个问题是针对旧版本。



给我一​​些想法这个问题。

解决方案

这是我如何实现我的。如果我在某处犯了什么错误,我很抱歉。我重构它,使它看起来不像我的生产代码。

在HTML中,我有:

 < form name =fileUploadFormmethod =POSTenctype =multipart / form-dataaction =@ routes.Application.upload()> 
档案1:< br /> < input type =filename =filePart1id =filePart1>< br />
档案2:< br /> < input type =filename =filePart2id =filePart1>< br />
< / form>

在我的控制器中,我有:

<$ p (); $ p $ {code> public static Result result(){
MultipartFormData body = request()。body()。asMultipartFormData();

FilePart filePart1 = body.getFile(filePart1);
FilePart filePart2 = body.getFile(filePart2);

文件newFile1 =新建文件(计算机中的路径);
文件newFile2 =新文件(计算机中的路径);

文件file1 = filePart1.getFile();
文件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();





$ p就像Kris说的,你必须得到Apache的CommonIO



您可以轻松地将这个添加到您在/ PlayProject / project中找到的Build.scala中:

  import sbt._ 
import Keys._
import play.Project._
import com.typesafe.config._
$ b $ object ApplicationBuild extends构建{
val appDependencies = Seq(
commons-io%commons-io%2.4//在此添加

}

在此实现中,您可以将文件存储在您在文件中指定的任何位置的文件newFile1 。但是如果你想列出你的文件,你将不得不使用一个数据库。但是,您只需将文件路径作为String(varchar)存储在数据库中即可。我将把这部分留给你弄清楚,因为我不知道你想如何处理文件检索。

您可以限制用户只使用Javascript上传某些类型的文件。通过检查文件名来让Javascript进行表单验证:下面是一个例子:

 < script> 
var file1 = document.getElementById(filePart1)。value;
if(file1.indexOf(。pdf)== -1){
alert(Not a PDF file!);
else {
document.fileUploadForm.submit();
}
< / script>

希望所有这些都有帮助。


i am using play framework 2.1.2 using java and i am creating view to upload multiple files and my code is 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 to upload only doc and pdf file.

how to restrict form to upload only doc and pdf file ?

i can this with java but i am looking for html code.

after this i want to store multiple file to permanent storage in my computer.

and print name of file i 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 is storing file into temp directory but i want to store to permanent location not on temp directory as a file not temp file like if i upload a.docx i want to store this file into storage with a.docx name.

i don't want to store file into database.

and how to list all file that i uploaded by file name?

i found some question but i am not getting that answers because that question is for old version.

give me some idea to fix this issue.

解决方案

Here is how I implemented mine. I apologize if I made any mistakes somewhere. I "refactored" it so that it doesn't look anything like my production code.

In HTML I 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 my controller I 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 like Kris said, you will have to get Apache's CommonIO

You can easily do this buy adding this into your 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 this here
  )
}

In this implementation, you can store the files anywhere on your computer where you specified in File newFile1. But you will have to use a database if you want to list your files. But you only have to store the file path as a String (varchar) in the database. I will leave that part up to you to figure out as I don't know how you want to handle file retrieval.

You can restrict user to only upload certain type of files by using Javascript. Have Javascript do form validation by checking the file name: Here is an example:

<script>
    var file1 = document.getElementById("filePart1").value;
    if (file1.indexOf(".pdf") == -1) {
        alert("Not a PDF file!");
    else {
        document.fileUploadForm.submit();
    }
</script>

Hope all of that helps.

这篇关于如何使用播放框架上传多个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆