You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Khristian <de...@gmail.com> on 2009/11/18 20:28:47 UTC

[fileupload] need help dealing with large files!

Hello,
I'm trying to limit the size of files users can upload in my app, but
I can't figure out what to do when a user tries to upload some file
larger than the limit.
The limit is set in the web.xml file. Any file smaller than the limit
is handled without problem; when I try to upload a larger file, I
never get a response (not a exception, nor error, nor timeout).
How can I handle this situation correctly?
TIA,
--
Khristian Alexander Schönrock
http://derkosak.blogspot.com - Meu blógue!

Re: [fileupload] need help dealing with large files!

Posted by Khristian <de...@gmail.com>.
Hello Andreas,
I included the phase listener as you instructed, and it does intercept
the rendering. However, I can't seem to actually DO something to
interrupt the request
What does work is to send a redirect to another page, but then I can't
set the error messages :P
I changed your code and it's now like this:
/*-----------------------**/
public void beforePhase(PhaseEvent event) {
		// RENDER_RESPONSE Phase
		try {
			//			if (event.getPhaseId().equals(PhaseId.RENDER_RESPONSE)) {

			FacesContext fc = FacesContext.getCurrentInstance();
			HttpServletRequest req = (HttpServletRequest)
fc.getExternalContext().getRequest();
			String message = null;
			if (req instanceof MultipartRequestWrapper) {
				MultipartRequestWrapper mpreq = (MultipartRequestWrapper) req;
				Object m = mpreq.getAttribute("org.apache.myfaces.custom.fileupload.exception");
				FileItem ff = mpreq.getFileItem("formulario:documento");//the file
being uploaded
				if (m != null) {
					message = m.toString().toLowerCase();
				}
				if ((message != null) && (message.indexOf("sizelimitexceeded") > -1)) {
					MessageUtils.addMessage(FacesMessage.SEVERITY_ERROR,
"msg_FileSizeLimitExceeded", null);
				} else {
					NavigationHandler nh = fc.getApplication().getNavigationHandler();
					if (ff != null) {
						if (ff.getSize() > 2097152) {
                                                        ...
							MessageUtils.addMessage(FacesMessage.SEVERITY_ERROR, "MSG1", null);
						}
					} else {
						MessageUtils.addMessage(FacesMessage.SEVERITY_ERROR, "MSG2", null);
						HttpServletResponse resp = (HttpServletResponse)
fc.getExternalContext().getResponse();
						resp.sendRedirect("...same page...");
					}
/*-----------------------**/
I noticed that this exception attribute never gets set, so I use the
FileItem to decide what to do. If it's below the size limit, ff is not
null. If it exceeds the limit, the FileItem is then null.
How can I tell Faces to stop trying to process this request and go
back to the previous page, while maintaining the error messages?
Thanks,
--
Khristian Alexander Schönrock
http://derkosak.blogspot.com - Meu blógue!



On Thu, Nov 19, 2009 at 5:41 AM,  <An...@hella.com> wrote:
> Hello Khristian,
>
> you have to get the error in the phase listener (Render Phase in before
> Phase).
>
> public class myPhaseListener implements PhaseListener {
>
> ...
>
>        public void beforePhase(PhaseEvent event) {
>
>                // RENDER_RESPONSE Phase
>                if (event.getPhaseId().equals(PhaseId.RENDER_RESPONSE)){
>
>                        //error fileupload ?
>                        Object req =
> context.getExternalContext().getRequest();
>                        String message=null;
>                        if (req instanceof MultipartRequestWrapper) {
>                                MultipartRequestWrapper mpreq =
> (MultipartRequestWrapper) req;
>                                Object m =
> mpreq.getAttribute("org.apache.myfaces.custom.fileupload.exception");
>                                if(m!=null)
> message=m.toString().toLowerCase();
>                                if (message!= null &&
> message.indexOf("sizelimitexceeded")>-1) {
>
> MessageUtils.addMessage(FacesMessage.SEVERITY_ERROR,"msg_FileSizeLimitExceeded",null);
>                                }
>                        }
>
>
>
>                }
>
>        }
>
> }
>
>
> best regards
> Andreas
>
>
>
>
> Khristian <de...@gmail.com>
>
> 18.11.2009 20:28
> Bitte antworten an
> "MyFaces Discussion" <us...@myfaces.apache.org>
>
>
> An
> users@myfaces.apache.org
> Kopie
>
> Thema
> [fileupload] need help dealing with large files!
>
>
>
>
>
>
> Hello,
> I'm trying to limit the size of files users can upload in my app, but
> I can't figure out what to do when a user tries to upload some file
> larger than the limit.
> The limit is set in the web.xml file. Any file smaller than the limit
> is handled without problem; when I try to upload a larger file, I
> never get a response (not a exception, nor error, nor timeout).
> How can I handle this situation correctly?
> TIA,
> --
> Khristian Alexander Schönrock
> http://derkosak.blogspot.com - Meu blógue!
>
>

Antwort: [fileupload] need help dealing with large files!

Posted by An...@hella.com.
Hello Khristian,

you have to get the error in the phase listener (Render Phase in before 
Phase).

public class myPhaseListener implements PhaseListener {

...

        public void beforePhase(PhaseEvent event) {

                // RENDER_RESPONSE Phase
                if (event.getPhaseId().equals(PhaseId.RENDER_RESPONSE)){

                        //error fileupload ?
                        Object req = 
context.getExternalContext().getRequest();
                        String message=null;
                        if (req instanceof MultipartRequestWrapper) {
                                MultipartRequestWrapper mpreq = 
(MultipartRequestWrapper) req;
                                Object m = 
mpreq.getAttribute("org.apache.myfaces.custom.fileupload.exception");
                                if(m!=null) 
message=m.toString().toLowerCase();
                                if (message!= null && 
message.indexOf("sizelimitexceeded")>-1) {
 
MessageUtils.addMessage(FacesMessage.SEVERITY_ERROR,"msg_FileSizeLimitExceeded",null);
                                }
                        }



                }

        }

}


best regards 
Andreas




Khristian <de...@gmail.com>

18.11.2009 20:28
Bitte antworten an
"MyFaces Discussion" <us...@myfaces.apache.org>


An
users@myfaces.apache.org
Kopie

Thema
[fileupload] need help dealing with large files!






Hello,
I'm trying to limit the size of files users can upload in my app, but
I can't figure out what to do when a user tries to upload some file
larger than the limit.
The limit is set in the web.xml file. Any file smaller than the limit
is handled without problem; when I try to upload a larger file, I
never get a response (not a exception, nor error, nor timeout).
How can I handle this situation correctly?
TIA,
--
Khristian Alexander Schönrock
http://derkosak.blogspot.com - Meu blógue!