2011-10-03 48 views
5

我在使多个文件上传工作时遇到了一些问题。当我选择x个文件时,它会成功完成,但第一个文件将被上传x次,而其他文件则不会被上传。任何人都可以指出我做错了什么?在playframework上多文件上传

形式:

#{form @Projects.uploadPictures(project.id), enctype:'multipart/form-data'} 

<p> 
    <label>&{'title'}</label> 
    <input type="text" name="title"/> 
    <strong>(&{'addPicture.chooseTitle'})</strong> 
</p> 
<p> 
    <label>&{'Pictures'}</label> 
    <input type="file" multiple name="files" id="files"/> 
</p> 
<p> 
    <input type="submit" value="&{'publish'}" /> 
</p> 

#{/form} 

处理的文件:

public static void uploadPictures(long id, String title, List<Blob> files) { 
    String error = "";   
    if(files != null && !title.trim().equals("")) { 
     Project project = Project.findById(id); 
     // Save uploaded files 
     Picture picture; 

     for(int i = 0; i<files.size(); i++) { 
      if(files.get(i) != null) { 
       System.out.println("i: "+i+"\nFiltype: "+files.get(i).type()); 
       if(files.get(i).type().equals("image/jpeg") || files.get(i).type().equals("image/png")) { 
        picture = new Picture(project, title+"_bilde_"+(i+1), files.get(i)); 
        project.addPicture(picture); 
       } else { 
        error += "Fil nummer "+(i+1)+" er av typen "+files.get(i).type()+" og ikke av typen .JPG eller .PNG og ble dermed ikke lagt til. \n"; 
       } 
      } else { 
       error = "Ingen filer funnet"; 
      } 
     } 
    } else { 
     error = "Velg en tittel for bildene"; 
    } 
    if(error.equals("")) { 
     flash.success("Picture(s) added"); 
    } else { 
     flash.error(error); 
    } 
    addPicture(id); 
} 

回答

3

明白了这样的工作,如果有人是有史以来感兴趣:

public static void uploadPictures(long id, String title, File fake) { 
    List<Upload> files = (List<Upload>) request.args.get("__UPLOADS"); 
    if(files != null) { 
     Project project = Project.findById(id); 
     Picture picture; 
     Blob image; 
     InputStream inStream; 
     for(Upload file: files) { 
      if(file != null) { 
       try { 
        inStream = new java.io.FileInputStream(file.asFile()); 
        image = new Blob(); 
        image.set(inStream, new MimetypesFileTypeMap().getContentType(file.asFile())); 
        picture = new Picture(project, file.getFileName(), image); 
        project.addPicture(picture); // stores the picture 
       } catch (FileNotFoundException e) { 
        System.out.println(e.toString()); 
       } 
      } 
     } 
    } 
    addPicture(id); //renders the image upload view 
} 

会很乐意让与的Blob对象的数组,而不必request.args.get(“__上传”工作解决方案) 如果可能的话。

+0

你有没有看到这个:P - http://stackoverflow.com/questions/7401364/multi-file-upload-with-play/7571000#7571000? – Rifat

+0

你能接受你的答案吗?谢谢。 –

-1

应该<input type="file" multiple name="files" id="files"/>不是:<input type="file multiple" name="files" id="files"/>

其次,你究竟在哪里保存图像?我认为你应该把它保存在你的循环中,你放project.addPicture(picture);,但实际上它看起来像在最后一行中保存到系统:addPicture(id);这有点解释了为什么它保存相同的图像(最后一个或第一个确定他们如何被解析))多次。

+0

感谢您的回答,但没有呈现为多个文件上传。 project.addPicture(图片)保存图像,addPicture(id)只呈现图像上传的视图。任何其他想法? – vegardoj

+0

绝对不是。 ['multiple'是一个单独的属性](http://www.w3.org/TR/html-markup/input.file.html#input.file.attrs.multiple),而不是'type'的值的一部分。 –

2

所以,你可以使用@as到参数的时处理绑定到特定的播放TypeBinder

这个

所以:

public static void chargedMultiUpload(@As(binder = FileArrayBinder.class) Object xxx) throws IOException{ ... } 

而且这个网站

<input type="file" multiple name="files" id="files"/> 

所以,你必须使用File [] doo =(File [])xxx;

+1

您可以创建自己的活页夹,以便控制器更清洁 –