2013-01-11 32 views
5

Vì vậy, những gì tôi có là một DataView với ba cột, một trong số đó là cột hộp kiểm cho phép người dùng kiểm tra tệp nào họ muốn tải xuống.Wicket: tải xuống các tập tin sau khi onSubmit

Vì mục đích đơn giản (tôi nghĩ); Tôi đã quyết định để nén các tập tin vào một tập tin zip duy nhất và phục vụ nó sau khi nó được tạo ra.

Dưới đây là những gì tôi có cho đến nay:

Code::


Button downloadLogButton = new Button("downloadlogbutton") { 
     private static final long serialVersionUID = 1L; 
     @Override 
     public void onSubmit() { 
      // Some utility class I made that zips files 
      LogUtility util = new LogUtility(); 
      util.zipLogFiles("sample", logs); 
     } 
    }; 

    Form logsForm = new Form("logsform") { 

    }; 

    logsForm.add(downloadLogButton); 

    CheckGroup<File> checkGroup = new CheckGroup<File>("logscheckgroup", new ArrayList<File>()); 
    WebMarkupContainer logsViewContainer = new WebMarkupContainer("datatable"); 
    DataView<File> logsView = new DataView<File>("logrows", new ListDataProvider<File>(logs)) { 

     private static final long serialVersionUID = 1L; 

     public void populateItem(final Item<File> item) { 
      final File file = (File) item.getModelObject(); 
      item.add(new Check<File>("logdownloadcheck", item.getModel())); 
      item.add(new Label("logname", file.getName())); 
      SimpleDateFormat sdf = new SimpleDateFormat("E, dd MMM yyyy hh:mm a"); 
      item.add(new Label("logdatemodified", sdf.format(file .lastModified()))); 
     } 
    }; 

    logsViewContainer.add(logsView); 
    checkGroup.add(logsViewContainer); 
    logsForm.add(checkGroup); 
    add(logsForm); 

Làm thế nào để phục vụ các tập tin zip sau khi nó được tạo ra để tải về? Những lựa chọn của tôi là gì? Tôi muốn tránh phải chuyển hướng họ đến trang xác nhận hoặc trang Your download is ready.

CẬP NHẬT

Dựa trên Xavi López câu trả lời, tôi đã thêm đoạn mã sau vào onSubmit chức năng Button 's của tôi.

org.apache.wicket.util.file.File log = new org.apache.wicket.util.file.File("/home/keeboi/Downloads/sample.zip"); 

IResourceStream resourceStream = new FileResourceStream(log); 
IRequestHandler target = new ResourceStreamRequestHandler(resourceStream); 

getRequestCycle().scheduleRequestHandlerAfterCurrent(target); 

Và tôi nhận được HTTP Error 404: Not Found.

Trả lời

5

Bạn có thể làm giống như DownloadLink và tạo FileResourceStream từ tệp được nén. Sau đó, chỉ cần thay đổi mục tiêu của chu kỳ yêu cầu hiện tại:

Button downloadLogButton = new Button("downloadlogbutton") { 
    private static final long serialVersionUID = 1L; 
    @Override 
    public void onSubmit() { 
     // Some utility class I made that zips files 
     LogUtility util = new LogUtility(); 
     util.zipLogFiles("sample", logs); 
     IResourceStream resourceStream = new FileResourceStream(
      new org.apache.wicket.util.file.File(someFile)); // Use whatever file you need here 
     IRequestTarget t = new ResourceStreamRequestTarget(stream){ 
      @Override 
      public String getFileName() { 
       return "filename.zip"; 
      } 
     } 
     getRequestCycle().setRequestTarget(t); 
    } 
}; 

Nếu bạn muốn xóa các tập tin sau khi tải về, bạn có thể ghi đè lên IRequestTarget#respond(RequestCycle) như thế này:

@Override 
public void respond(RequestCycle requestCycle) { 
    super.respond(requestCycle); 
    // Delete the file 
    ((FileResourceStream)getResourceStream()).getFile().delete(); 
} 

Câu hỏi liên quan sau đây cũng có thể hữu ích: How to use Wicket's DownloadLink with a file generated on the fly?.

+1

Vì tôi đang sử dụng Wicket 1.5, tôi đã làm như sau: 'org.apache.wicket.util.file.File log = new org.apache.wicket.util.file.File ("/home/keeboi/Tải xuống/sample.zip "); IResourceStream resourceStream = new FileResourceStream (nhật ký); IRequestHandler target = new ResourceStreamRequestHandler (resourceStream); getRequestCycle(). ScheduleRequestHandlerAfterCurrent (target); ' Nhưng tôi nhận được 'Lỗi HTTP 404: Không tìm thấy' –

+0

Được rồi, đã xảy ra lỗi trong lớp tiện ích của tôi. Cảm ơn! –

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