我如何使用&lt; p:文件上传&GT;用简单的方式和Ajax =&QUOT;真&QUOT;? [英] How can I use <p:fileUpload> with mode simple and ajax="true"?
问题描述
我要上传与使用PrimeFaces和ManagedBean的文件。我想用号码:文件上传
与模式=简单
I want to upload a file with use of PrimeFaces and a ManagedBean. I want use p:fileUpload
with mode="simple".
XHTML code:
<p:fileUpload id="fileId" mode="simple" value="#{itemBean.upFile}"
fileLimit="1" />
<p:commandButton ajax="true" value="Upload File" update="messagess"
id="save-btn"
actionListener="#{itemBean.fileUpload(itemBean.upFile,itemBean.hiddenFileName)}"
process="@this" oncomplete="showImage()" />
ManagedBean:
public void fileUpload(UploadedFile uploadFile, String hiddenKey) {
String keyFileName = hiddenKey;
boolean validFile = true;
String expression = "([^\\s]+(\\.(?i)(gif|jpg|jpeg|gif|png|PNG|GIF|JPG|JPEG|bmp))$)";
if((uploadFile == null) ) {
validFile = false;
FacesMessage msg = new FacesMessage("Error! "+ "Please select an image.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
else {
System.out.println("going to file upload"+uploadFile.getFileName()+"---hiddenKey"+keyFileName);
if((!uploadFile.getFileName().matches(expression)) ) {
validFile = false;
FacesMessage msg = new FacesMessage("Error! "+ uploadFile.getFileName() + " is not an image.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
if(uploadFile.getSize() > 1000000) {
validFile = false;
FacesMessage msg = new FacesMessage("Error! "+ uploadFile.getFileName() + " size is too large.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
if (validFile) {
// Do what you want with the file
try {
//String extn =uploadFile.getFileName().substring(uploadFile.getFileName().lastIndexOf("."));
copyFile(uploadFile.getFileName(), uploadFile.getInputstream());
FacesMessage msg = new FacesMessage("Success! "+ uploadFile.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
} catch (IOException e) {
e.printStackTrace();
FacesMessage msg = new FacesMessage("Error! "+ uploadFile.getFileName()+ " not uploaded.");
FacesContext.getCurrentInstance().addMessage(null, msg);
}
}
}
}
我的问题是,我点击了支持bean的命令按钮和方法不被调用。
如果我使用阿贾克斯=假
该方法被调用,但页面刷新了。
My problem is, that I clicked on the command button and method in the backing bean is not called.
If I use ajax="false"
the method is called, but the page got refreshed.
我如何使用阿贾克斯=真正的
和&LT;电话号码:?文件上传&GT;
一起
推荐答案
在&LT;电话号码:文件上传模式=简单&GT;
不支持AJAX。很抱歉,但是这是故事的结束
The <p:fileUpload mode="simple">
doesn't support ajax. Sorry, but this is end of story.
如果切换到兼容AJAX的&LT;电话号码:文件上传模式=高级&GT;
真的不是一种选择,那么你最好的选择是升级到JSF 2.2并利用其新的本地&LT; H:INPUTFILE&GT;
组件代替。它也显示在浏览器的默认look'n'feel并能够通过一个隐藏的iframe招模拟AJAX经验。
If switching to the ajax-compatible <p:fileUpload mode="advanced">
is really not an option, then your best bet is upgrading to JSF 2.2 and use its new native <h:inputFile>
component instead. It also shows up in browser default look'n'feel and is capable of simulating the ajax experience via a hidden iframe trick.
- <一个href="http://stackoverflow.com/questions/8875818/how-to-use-primefaces-pfileupload-listener-method-is-never-invoked/8880083#8880083">How使用PrimeFaces号码:文件上传?监听方法永远不会被调用
无关的具体问题,在你的文件上传()
操作方法,这两个参数是完全没有必要的。就在
Unrelated to the concrete problem, those two arguments in your fileUpload()
action method are completely unnecessary. Just the
action="#{itemBean.fileUpload}"
与
public void fileUpload() {
// ...
}
作品一样好,一旦你删除程序=@这个
属性。
这篇关于我如何使用&lt; p:文件上传&GT;用简单的方式和Ajax =&QUOT;真&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!