[英]JavaFX AnimationTimer doesn't start when it is called as a result of a Button click
[英]Download doesn't start on button click
可以使用下载文件吗
AjaxLink<Void> downloadButton = new AjaxLink<Void>("downloadButton") {
@Override
public void onClick(AjaxRequestTarget ajaxRequestTarget) {
}
? 还是只能通过onSubmit()
操作发生? 因为我有一个通过单击按钮下载文件的工作代码,但该方法使用onSubmit
。 现在我正在尝试使用这种按钮执行相同的操作,它以相同的方式准备文件,甚至在我的本地文件中创建一个临时文件,但是当涉及到浏览器中的弹出窗口时,它什么也不做. 可能是什么问题呢?
编辑:
这是工作代码,但如果我在我的 AjaxLink 中使用相同的代码,它就不能如前所述正常工作:
@Override
protected void onSubmit() {
super.onSubmit();
File file = null;
try {
file = File.createTempFile("temp-file-name", ".csv");
String data = getData();
if (data == null) {
FileUtils.writeByteArrayToFile(file, ("").getBytes());
} else {
FileUtils.writeByteArrayToFile(file, data.getBytes());
}
} catch (IOException e) {
e.printStackTrace();
}
if (file == null) {
throw new IllegalStateException(getClass().getName() +
" failed to retrieve a File object from model");
}
final File preparedFile = file;
IResourceStream resourceStream = new FileResourceStream(
new org.apache.wicket.util.file.File(file));
getRequestCycle().scheduleRequestHandlerAfterCurrent(
new ResourceStreamRequestHandler(resourceStream) {
@Override
public void respond(IRequestCycle requestCycle) {
super.respond(requestCycle);
Files.remove(preparedFile);
}
}.setFileName("Report.csv")
.setContentDisposition(ContentDisposition.ATTACHMENT));
}
}
}
您不能只在 Ajax 响应中的 stream 文件。
您需要使用 Wicket 的 AjaxDownloadBehavior。 它提供了克服这个限制的逻辑。
Demo code: https://github.com/apache/wicket/blob/0ba3ce015ae9f258246b92ac599a00481a26c37f/wicket-examples/src/main/java/org/apache/wicket/examples/ajax/builtin/AjaxDownloadPage.java Demo in action: http: //examples8x.wicket.apache.org/ajax/download
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.