我如何使用< p:文件上传>用简单的方式和Ajax ="真"? [英] How can I use <p:fileUpload> with mode simple and ajax="true"?

查看:132
本文介绍了我如何使用< p:文件上传>用简单的方式和Ajax ="真"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要上传与使用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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆