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
.
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' –
Được rồi, đã xảy ra lỗi trong lớp tiện ích của tôi. Cảm ơn! –