[Webtest] File upload

webtest@lists.canoo.com webtest@lists.canoo.com
Wed, 16 Oct 2002 12:28:30 +0100


I'm very new to Canoo and HttpUnit, so please bear with me...

There seems to be a problem with submitting forms that contain input boxes
of type "file". I always got an exception when doing a ClickButton after
using setInputField. I have modified ClickButton.java so that it works for
me. There are 2 problems, however. First, I can't use CVS to submit this
contribution in the usual way (I mean I can't use CVS here at all, not that
there is a problem with it), and 2nd, I assume any field called "file" is a
file input field, which is obviously somewhat lacking.

If anyone can tell me how this could be done more generally, I'd be happy
to fix it up properly (assuming I ever get CVS...).

Reg Skelton

add:
     import com.meterware.httpunit.UploadFileSpec

replace (lines 135-158):
    private void addParameterToRequest(WebRequest request, ParameterHolder
parameterHolder) {

        List parameterValues = parameterHolder.getValueList();
        if(parameterHolder.isPreserveExistingValue()) {
            String[] existingParameters =
request.getParameterValues(parameterHolder.getName());
            parameterValues.addAll(Arrays.asList(existingParameters));
        }

          request.setParameter(parameterHolder.getName(),
                  (String []) parameterValues.toArray(new
String[parameterValues.size()]));
    }
with:
    private void addParameterToRequest(WebRequest request, ParameterHolder
parameterHolder) {

        List parameterValues = parameterHolder.getValueList();
        if(parameterHolder.isPreserveExistingValue()) {
            String[] existingParameters =
request.getParameterValues(parameterHolder.getName());
            parameterValues.addAll(Arrays.asList(existingParameters));
        }

        if( "file".equals( parameterHolder.getName()))
        {
          UploadFileSpec[] ufs= new UploadFileSpec[parameterValues.size()];

          for( int i=0; i < ufs.length; i++)
          {
            ufs[i]= new UploadFileSpec( new java.io.File( (String)
parameterValues.get( i)));
          }

          request.setParameter(parameterHolder.getName(), ufs);
        }
        else {
          request.setParameter(parameterHolder.getName(),
                  (String []) parameterValues.toArray(new
String[parameterValues.size()]));
        }
    }


This communication is for informational purposes only.  It is not intended as
an offer or solicitation for the purchase or sale of any financial instrument
or as an official confirmation of any transaction. All market prices, data
and other information are not warranted as to completeness or accuracy and
are subject to change without notice. Any comments or statements made herein
do not necessarily reflect those of J.P. Morgan Chase & Co., its
subsidiaries and affiliates.