You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Raghu <te...@gmail.com> on 2008/12/05 18:35:25 UTC

Struts2 Ajax File Upload

Hi,
I am trying to implement Ajax File Upload using JQuery's Form Plugin which
uses iframe internally. JQuery's Form Plugin doc says it support response
type of HTML or XML.

Without Ajax, my file upload works fine. I am not sure what result type to
choose for ajax response.

If I use type="stream" then I am getting below exception... Any idea how to
resolve this issue...
I saw ajax file upload plug-in's example struts.xml and it is using type as
httpheader but I could not understand how to use that.
I have few validations like file type, file size etc and I want to stream
back these validation messages..

   <action name="myPhotoUpload_*" method="{1}"
class="com.rawatsoft.write4smile.webapp.action.FileUploadAction">
            <result name="input"
type="dispatcher">/WEB-INF/jsp/MyPhoto.jsp</result>
            <result name="success" type="stream">
                <param name="contentType">text/html</param>
                <param name="inputName">inputStream</param>
            </result>
  </action>


[WRITE4SMILE] ERROR [2008/12/05 09:53:59] | Servlet.service() for servlet
default threw exception
java.lang.IllegalArgumentException: Can not find a java.io.InputStream with
the name [inputStream] in the invocation stack. Check the <param
name="inputName"> tag specified for this action.
    at
org.apache.struts2.dispatcher.StreamResult.doExecute(StreamResult.java:189)
    at
org.apache.struts2.dispatcher.StrutsResultSupport.execute(StrutsResultSupport.java:178)
    at
com.opensymphony.xwork2.DefaultActionInvocation.executeResult(DefaultActionInvocation.java:348)
    at
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:253)
    at
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:50)
    at
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:504)
    at
org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:421)
    at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
    at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)
    at
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)
    at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
    at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)
    at
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)
    at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:215)
    at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
    at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
    at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
    at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
    at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
    at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
    at
org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
    at
org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
    at
org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
    at
org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
    at
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
    at java.lang.Thread.run(Unknown Source)

Re: Struts2 Ajax File Upload

Posted by Raghu <te...@gmail.com>.
Please don't reply to this mail. I have solved issue and it work the way I
expect. It is surprising that in case of file upload JQuery's Form Plugin is
not picking form's action attribute value and request was not reaching to my
action class. When I explicitly pass url's value to ajaxForm("options")
like below then request reaches action class and everythings works perfectly
for me. May be it has something to do with iframe, as my other ajax forms
work fine.

$('#myPhotoUploadForm').ajaxForm({
                    url:  'myPhotoUpload_upload.action',

Thanks...

On Fri, Dec 5, 2008 at 1:21 PM, Raghu <te...@gmail.com> wrote:

> Thanks Dave for prompt reply.
> Yes, I have defined inputStream property in action class like below.
>
> *private InputStream inputStream;
>
> public InputStream getInputStream() {
>         return inputStream;
>     }
>
>     public void setInputStream(InputStream inputStream) {
>         this.inputStream = inputStream;
>     }
> *
>
> One more thing that I missed in earlier message, I am getting exception
> even before my action method "upload" getting invoked. I have few System.Out
> in method, I don't see them in log..it seems to me request is failing before
> upload method call...
>
>  public String upload() {
>
>         try{
>             UserTO userTo = this.getCurrentUser();
>
>             System.out.println("myPhotoFileName:"+myPhotoFileName);
>             System.out.println("myPhoto:"+myPhoto);
>             System.out.println("getMyPhotoContentType:"+
> this.getMyPhotoContentType());
>
>             if ((myPhotoFileName != null && "".equals(myPhotoFileName)) ||
> myPhoto == null) {
>
>                 inputStream = new
> StringBufferInputStream(this.getText("required.userPhoto"));
>
>                 return Action.SUCCESS;
>             } else if (myPhoto.length() > 200000) {
>
>                 inputStream = new
> StringBufferInputStream(this.getText("userPhoto.maxLengthExceeded"));
>                 return Action.SUCCESS;
>             }
>             if (!getMyPhotoContentType().startsWith("image/")) {
>                 inputStream = new
> StringBufferInputStream(this.getText("userPhoto.invalidPhotoType"));
>
>                 return Action.SUCCESS;
>             }
>
>            // Some File Upload logic...
>
>             inputStream = new StringBufferInputStream(myPhotoFileName + "
> added to your profile.");
>         }catch(Exception exp){
>             LOG.error("Exception in Photo Upload:", exp);
>             inputStream = new StringBufferInputStream("Photo upload failed.
> Please try again later.");
>         }
>         return Action.SUCCESS;
>
>     }
>
>
> On Fri, Dec 5, 2008 at 12:45 PM, Dave Newton <ne...@yahoo.com>wrote:
>
>> --- On Fri, 12/5/08, Raghu wrote:
>> > I am trying to implement Ajax File Upload using
>> > JQuery's Form Plugin which uses iframe internally.
>>  JQuery's Form Plugin doc says it support response
>> > type of HTML or XML.
>> >
>> > Without Ajax, my file upload works fine. I am not sure what
>> > result type to choose for ajax response.
>> >
>> > If I use type="stream" then I am getting below
>> > exception... Any idea how to resolve this issue...
>> > [...]
>> > > [WRITE4SMILE] ERROR [2008/12/05 09:53:59] |
>> > Servlet.service() for servlet default threw exception
>> > java.lang.IllegalArgumentException: Can not find a
>> > java.io.InputStream with the name [inputStream] in
>> > the invocation stack. Check the <param name="inputName">
>> > tag specified for this action.
>>
>> Surprisingly, the steps for solving this particular issue are described
>> completely in the exception message: if you're going to use a stream result
>> you need to supply and input stream in your action, the property name of
>> said stream being supplied by the "inputName" param.
>>
>> The contents of that stream would be decided by whatever jQuery's form
>> plugin wants--which I don't know. It sounds like you'd need to craft a
>> response containing whatever information the form plugin wants; if you can
>> qualify those requirements it'll be easier to help.
>>
>> Dave
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
>> For additional commands, e-mail: user-help@struts.apache.org
>>
>>
>

Re: Struts2 Ajax File Upload

Posted by Raghu <te...@gmail.com>.
Thanks Dave for prompt reply.
Yes, I have defined inputStream property in action class like below.

*private InputStream inputStream;

public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }
*

One more thing that I missed in earlier message, I am getting exception even
before my action method "upload" getting invoked. I have few System.Out in
method, I don't see them in log..it seems to me request is failing before
upload method call...

 public String upload() {

        try{
            UserTO userTo = this.getCurrentUser();

            System.out.println("myPhotoFileName:"+myPhotoFileName);
            System.out.println("myPhoto:"+myPhoto);
            System.out.println("getMyPhotoContentType:"+
this.getMyPhotoContentType());

            if ((myPhotoFileName != null && "".equals(myPhotoFileName)) ||
myPhoto == null) {

                inputStream = new
StringBufferInputStream(this.getText("required.userPhoto"));

                return Action.SUCCESS;
            } else if (myPhoto.length() > 200000) {

                inputStream = new
StringBufferInputStream(this.getText("userPhoto.maxLengthExceeded"));
                return Action.SUCCESS;
            }
            if (!getMyPhotoContentType().startsWith("image/")) {
                inputStream = new
StringBufferInputStream(this.getText("userPhoto.invalidPhotoType"));

                return Action.SUCCESS;
            }

           // Some File Upload logic...

            inputStream = new StringBufferInputStream(myPhotoFileName + "
added to your profile.");
        }catch(Exception exp){
            LOG.error("Exception in Photo Upload:", exp);
            inputStream = new StringBufferInputStream("Photo upload failed.
Please try again later.");
        }
        return Action.SUCCESS;
    }


On Fri, Dec 5, 2008 at 12:45 PM, Dave Newton <ne...@yahoo.com> wrote:

> --- On Fri, 12/5/08, Raghu wrote:
> > I am trying to implement Ajax File Upload using
> > JQuery's Form Plugin which uses iframe internally.
>  JQuery's Form Plugin doc says it support response
> > type of HTML or XML.
> >
> > Without Ajax, my file upload works fine. I am not sure what
> > result type to choose for ajax response.
> >
> > If I use type="stream" then I am getting below
> > exception... Any idea how to resolve this issue...
> > [...]
> > > [WRITE4SMILE] ERROR [2008/12/05 09:53:59] |
> > Servlet.service() for servlet default threw exception
> > java.lang.IllegalArgumentException: Can not find a
> > java.io.InputStream with the name [inputStream] in
> > the invocation stack. Check the <param name="inputName">
> > tag specified for this action.
>
> Surprisingly, the steps for solving this particular issue are described
> completely in the exception message: if you're going to use a stream result
> you need to supply and input stream in your action, the property name of
> said stream being supplied by the "inputName" param.
>
> The contents of that stream would be decided by whatever jQuery's form
> plugin wants--which I don't know. It sounds like you'd need to craft a
> response containing whatever information the form plugin wants; if you can
> qualify those requirements it'll be easier to help.
>
> Dave
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>

Re: Struts2 Ajax File Upload

Posted by Dave Newton <ne...@yahoo.com>.
--- On Fri, 12/5/08, Raghu wrote:
> I am trying to implement Ajax File Upload using
> JQuery's Form Plugin which uses iframe internally. 
 JQuery's Form Plugin doc says it support response
> type of HTML or XML.
> 
> Without Ajax, my file upload works fine. I am not sure what
> result type to choose for ajax response.
> 
> If I use type="stream" then I am getting below
> exception... Any idea how to resolve this issue...
> [...]
> > [WRITE4SMILE] ERROR [2008/12/05 09:53:59] |
> Servlet.service() for servlet default threw exception
> java.lang.IllegalArgumentException: Can not find a
> java.io.InputStream with the name [inputStream] in 
> the invocation stack. Check the <param name="inputName"> 
> tag specified for this action.

Surprisingly, the steps for solving this particular issue are described completely in the exception message: if you're going to use a stream result you need to supply and input stream in your action, the property name of said stream being supplied by the "inputName" param.

The contents of that stream would be decided by whatever jQuery's form plugin wants--which I don't know. It sounds like you'd need to craft a response containing whatever information the form plugin wants; if you can qualify those requirements it'll be easier to help.

Dave


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org