1

I searched the whole day about how to upload more files in HTML <input type="file" multiple/> tag with HtmlUnit in Java, but I didn't find out yet.
Can anyone help? I can't modify the web pages, because they're not mine.

I am currently using:

HtmlFileInput#setValueAttribute(path);
HtmlFileInput#setContentType(contenType);

Thank you so much!

1
  • 1
    Why getting 2 -1? (It's just one now, but still.) Am I missing something? Commented Oct 17, 2013 at 13:45

1 Answer 1

3

Ok. It seems that this feature does not exist in HtmlUnit 2.1.13, therefore a feature request ticket was opened: https://sourceforge.net/p/htmlunit/feature-requests/215/

Anyway, I found a way in which a similar behavior can be achieved. Here it is:

public static HtmlFileInput insertFileInputWithValue(String name, String value, 
    String contentType, HtmlPage page, DomElement parent) {
  // Necessary, otherwise HtmlUnit doesn't generate HtmlFileInput, but HtmlTextInput by default
  AttributesImpl ai = new AttributesImpl();
  ai.addAttribute(null, null, "type", null, "file");
  ai.addAttribute(null, null, "name", null, name);

  HtmlFileInput input = (HtmlFileInput) HTMLParser.getFactory("input")
    .createElementNS(page, null, "input", ai, true);
  input.setValueAttribute(value);
  input.setContentType(contentType);
  parent.appendChild(input);

  return input;
}

These piece of code injects a new html input file in whatever parent you specify, a form most probably.
Even though HtmlUnit doesn't allow using <input type="file" multiple/>, the same behavior can be achieved by injecting more HtmlFileInput elements with the same name in your form, in which you set as values the files you want to upload.

I hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.