2011-10-03 30 views
5

Tôi đang gặp một số vấn đề với việc tải lên nhiều tệp để hoạt động. Khi tôi chọn x tập tin, nó đi qua thành công, nhưng tập tin đầu tiên đang được tải lên x lần, và những người khác không được tải lên ở tất cả. Bất cứ ai có thể chỉ ra những gì tôi đang làm sai?Tải lên nhiều tệp trong playframework

Mẫu:

#{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} 

Xử lý các tập tin:

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); 
} 

Trả lời

3

Got nó hoạt động như thế này nếu có ai là không bao giờ quan tâm:

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 
} 

sẽ được hạnh phúc để có được một giải pháp làm việc với một mảng của các đối tượng Blob thay vì phải request.args.get ("__ UPLOADS") nếu có thể.

+0

Bạn có thấy điều này: P - http://stackoverflow.com/questions/7401364/multi-file-upload-with-play/7571000#7571000? – Rifat

+0

Bạn có thể chấp nhận câu trả lời của mình không? cảm ơn. –

-1

nên <input type="file" multiple name="files" id="files"/> không: <input type="file multiple" name="files" id="files"/>?

Thứ hai, bạn thực sự lưu hình ảnh ở đâu? Tôi nghĩ bạn nên lưu nó trong vòng lặp của bạn, nơi bạn đặt project.addPicture(picture);, nhưng thực sự có vẻ như hình ảnh được lưu vào hệ thống trong dòng cuối cùng của bạn: addPicture(id); Loại này giải thích lý do tại sao nó lưu cùng một hình ảnh (ảnh cuối cùng hoặc hình đầu tiên) chắc chắn cách chúng được phân tích cú pháp)) nhiều lần.

+0

Cảm ơn câu trả lời, nhưng không hiển thị khi tải lên nhiều tệp. project.addPicture (ảnh) lưu hình ảnh và addPicture (id) chỉ hiển thị khung nhìn để tải lên hình ảnh. Bất kỳ ý tưởng nào khác? – vegardoj

+0

Chắc chắn là không. ['multiple' là một thuộc tính riêng biệt] (http://www.w3.org/TR/html-markup/input.file.html#input.file.attrs.multiple) và không phải là một phần của giá trị cho' loại' . –

2

Vì vậy, bạn có thể sử dụng @As để ràng buộc việc xử lý một param để một vai cụ thể TypeBinder

Vì vậy, với điều này:

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

Và html này

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

Vì vậy, bạn phải thực hiện một diễn viên với một cái gì đó như File [] doo = (File []) xxx;

+1

Bạn có thể tạo chất kết dính của riêng mình để bộ điều khiển trở nên sạch hơn –

Các vấn đề liên quan