How to upload file of p:fileUpload using command button instead of upload button

后端 未结 1 1159
时光取名叫无心
时光取名叫无心 2021-01-14 21:12

Iam using JSF2.1 along with primefaces for uploading file.In my application i have to add the file dynamically on creating a record.But when i use i cant write the co

1条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-14 21:55

    That isn't possible with auto mode. Use the basic mode instead. Then you can bind the input value to an UploadedFile property directly. This only requires disabling Ajax.

    E.g.

    
        ...
        
        ...
        
    
    

    with

    private UploadedFile file; // +getter+setter
    
    public void save() {
        try (InputStream input = file.getInputStream()) {
            // ...
        }
    }
    

    The alternative is to migrate to standard JSF component which was introduced in JSF 2.2. Then you can continue using Ajax.

    E.g.

    
        ...
        
        ...
        
    
    

    with

    private Part file; // +getter+setter
    
    public void save() {
        try (InputStream input = file.getInputStream()) {
            // ...
        }
    }
    

    See also:

    • How to upload file using JSF 2.2 ? Where is the saved File?

    0 讨论(0)
提交回复
热议问题