2011-10-03 37 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

您可以創建自己的活頁夾,以便控制器更清潔 –