开发者

Seam: Validation of s:fileUpload?

I've got a JSF page that allows users to upload images. I'd like to perform some validation on the uploaded file to ensure it's the correct size, dimensions, content type, etc. I've created a JSF Validator, and have tried specifying it开发者_运维问答 both in the <s:fileUpload validator="XXX" /> attribute, as well as using the <f:validator /> tag. However, no matter what I try my validator is never called. Is there any way to validate a file upload in Seam? Would using <rich:fileUpload /> allow me to do validation on the uploaded file?


After a lot of trial and error, I've just decided to do a manual validation in my submit method. I haven't found a way to get a Validator object to work successfully.


You can try with a listener like (not sure if event has a cancel method to call if your constraint fails, but you can check):

public void listener(UploadEvent event) throws IOException {
    UploadItem item = event.getUploadItem();

    String name = "unnamed_attachment";
    byte[] data = item.getData();

    if (item.isFile()) {
        name = FilenameUtils.getName(item.getFileName());
        data = FileUtils.readFileToByteArray( item.getFile() );
    }

    debug("file uploaded '#0' : '#1'", name, item.getFileName());

And some constraints you can put in components.xml, like:

<web:multipart-filter create-temp-files="false" max-request-size="5200000" url-pattern="/*"/>

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜