You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Piotr Pikusa <Pi...@pbpolsoft.com.pl> on 2007/07/25 13:28:28 UTC

[Trinidad] How to check if file exists using InputFile component?

Hey!

As it is mentioned in the subject I wonder if there is any possibility to
check file existence. 
I got this code on my jsp page:

[code]

<tr:inputFile 
   required="true"
   rendered="true" 
   label="Load File:"
   value="#{backingBean.file}"
 />     
<af:commandButton
    text="#{"Button"}"
    action="#{backingBean.importFile}" />     

[/code]

which bind the value to the file property. The type of file is UploadFile.
In backingBean I have this code:

[code]

UploadFile file;

public void importFile(){
	if(file != null) {
		doSomeAction();		
	}
	else{
		returnError();
	}
}

public UploadedFile getFile() {
		return file;
	}

public void setFile(UploadedFile file) {
		this.file = file;
}

[/code]

The point of the problem is that the file is never null, no matter if file
exists or not. If file doesn't exist the InputFile component doesn't return
null. The UploadFile doesn't have a method to check file existence. 
So my question is when file property can be null. And how to check if file
exists using InputFile component and UploadFile object? (I cannot use other
type, but UploadFile).

Re: [Trinidad] How to check if file exists using InputFile component?

Posted by Matthias Wessendorf <ma...@apache.org>.
hi piotr,

the UploadedFile interfaces has a getLength() method

it's 0, when the file isn't there..

-M

On 7/25/07, Piotr Pikusa <Pi...@pbpolsoft.com.pl> wrote:
> Hey!
>
> As it is mentioned in the subject I wonder if there is any possibility to
> check file existence.
> I got this code on my jsp page:
>
> [code]
>
> <tr:inputFile
>    required="true"
>    rendered="true"
>    label="Load File:"
>    value="#{backingBean.file}"
>  />
> <af:commandButton
>     text="#{"Button"}"
>     action="#{backingBean.importFile}" />
>
> [/code]
>
> which bind the value to the file property. The type of file is UploadFile.
> In backingBean I have this code:
>
> [code]
>
> UploadFile file;
>
> public void importFile(){
>         if(file != null) {
>                 doSomeAction();
>         }
>         else{
>                 returnError();
>         }
> }
>
> public UploadedFile getFile() {
>                 return file;
>         }
>
> public void setFile(UploadedFile file) {
>                 this.file = file;
> }
>
> [/code]
>
> The point of the problem is that the file is never null, no matter if file
> exists or not. If file doesn't exist the InputFile component doesn't return
> null. The UploadFile doesn't have a method to check file existence.
> So my question is when file property can be null. And how to check if file
> exists using InputFile component and UploadFile object? (I cannot use other
> type, but UploadFile).
>


-- 
Matthias Wessendorf

further stuff:
blog: http://matthiaswessendorf.wordpress.com/
mail: matzew-at-apache-dot-org