The content-type (MIME type) of uploaded files is
hard-coded to "application/octet-stream;
charset=ISO-8859-1".
Instead, htmlunit should determine the content-type at
runtime similar to how most browsers (MSIE, Mozilla,
Opera) do it, then set the content-type when it uses
commons-httpclient to perform the form post.
A cursory look at the commons-httpclient-2.0 APIs and
htmlunit tells me that this won't be a trivial task.
Logged In: YES
user_id=257129
Since it's browser independant we're basicially a browser
that does it poorly :) Changing to a Feature Request.
Logged In: YES
user_id=84089
I figured out a way to add at least partial support for this
feature.
The java.net.URLConnection class has two methods that may
aid in the guessing of content type:
guessContentTypeFromName()
guessContentTypeFromStream()
Note that they are implementation dependent (the actual
classes implemented in J2SE-1.4.2 are in the sun.net.*
package hierarchy), but at least it's a start.
I would recommend using guessContentTypeFromStream() first
(assuming the file is more than 0 bytes in length). If that
returns null, then I would fall back to
guessContentTypeFromName().
If I have time, I'll write some test cases (or a test class)
and a patch to implement this feature.
W00t!
Logged In: YES
user_id=84089
Attaching patch to determine content type using the JVM
methods (as described in my previous comment). Includes
tests for GIF, JPG and PNG images.
Tests and code to implement JVM content type checking on uploaded files
Test files (in ZIP format) used by test code in patch
Logged In: YES
user_id=402164
I've fixed it a bit differently than the patch you proposed.
I've put the content type guess in the HtmlFileInput rather
than in the HttpWebConnection. Indeed it could be
interesting in a second time to enhance HtmlFileInput to
give control on this contentType.
Not sure if the test resources have to be placed with the
junit tests or not but in the first time it's ok.
Can you please check that it works as you expected: the
real good test for this fix would be against a real
webserver... but I haven't found time to setup the structure
to run tests against jetty (or something else).