HTML File Upload using enctype=multipart/form-data in form?

M

Matt

Should we use enctype=multipart/form-data in the form, I know it is recommended
if the form has <input type="file">. My form has <input type="file"> and other
html controls.

I tried the following,

form.html
=========
<FORM NAME="InputForm" ACTION="test.jsp" METHOD="POST" enctype=multipart/form-data>
<P><input type=text name="name" size=80>
<P><input type=file name="filename" size=80>
<P><input type="submit" value="Upload File Test">
</FORM>

test.jsp
=========
<p><%= "filename = " + request.getParameter("filename") %>
<p><%= "name = " + request.getParameter("name") %>


It will output
filename = null
name = null

I don't understand why, but if i remove enctype=multipart/form-data in the form,
then I am able to get the data.

Please advise. Thanks!!
 
T

Tim Slattery

Should we use enctype=multipart/form-data in the form, I know it is recommended
if the form has <input type="file">. My form has <input type="file"> and other
html controls.


I tried the following,

form.html
=========
<FORM NAME="InputForm" ACTION="test.jsp" METHOD="POST" enctype=multipart/form-data>
<P><input type=text name="name" size=80>
<P><input type=file name="filename" size=80>
<P><input type="submit" value="Upload File Test">
</FORM>

test.jsp
=========
<p><%= "filename = " + request.getParameter("filename") %>
<p><%= "name = " + request.getParameter("name") %>


It will output
filename = null
name = null

I don't understand why, but if i remove enctype=multipart/form-data in the form,
then I am able to get the data.

Because it's transmitted as a MIME stream, a very different format
then a normal POST, and request.getParameter doesn't know about MIME
streams. The other elements, filename and name, are there, but you
have to get them correctly.
 
M

Matt

Tim Slattery said:
Because it's transmitted as a MIME stream, a very different format
then a normal POST, and request.getParameter doesn't know about MIME
streams. The other elements, filename and name, are there, but you
have to get them correctly.


so I should still use enctype=multipart/form-data, and no need to
specify POST at all? If I cannot use request.getParameter(), how can I
get the data?

please advise. thanks
 
M

Malcolm Dew-Jones

Matt ([email protected]) wrote:

: > Because it's transmitted as a MIME stream, a very different format
: > then a normal POST, and request.getParameter doesn't know about MIME
: > streams. The other elements, filename and name, are there, but you
: > have to get them correctly.


: so I should still use enctype=multipart/form-data, and no need to
: specify POST at all? If I cannot use request.getParameter(), how can I
: get the data?

: please advise. thanks

-1-

The form must use both enctype=multipart/form-data and method=POST

Those settings control how the browser sends the data.

The browser can not send the file's data unless you use those settings, so
they must be used.

-2-

As a consequence, the data must be received by a method that accepts post
data.

I have not had to receive this type of data in java, so I do not know the
normal way to do it in java. I would hope that the servlet definitions
includes a standard way to receive this data, but I don't have the
relevent reference manuals with me here so I can't look it up for you (I
would if I could cause I ought to know how to do this).

In perl, the CGI module understands the different enctypes and decodes the
posted data no matter what enctype is used. Simple parameter values are
then accessed the usual way. Any uploaded file data is provided to the
program via a function that is specific to this task.

Since this seams a reasonable way to do it, I would make a wild guess and
look to see if the servlet definitions for the http post handler include
some kind of method that is specificly designed to return any upload file
data.

If the servlet cannot return this data then you would need to parse the
"raw" post input. The data is in mime format, so a mime parser should be
able to pull out the pieces.

I do not off hand know how to get the raw post data within a servlet. I
do not know what java package to use to parse mime data.
 
Joined
Oct 23, 2007
Messages
1
Reaction score
0
Hi Matt,
Malcolm is right with his suggestion. When you use multipart/form-data the form is not encrypted the regular way like

POST www.xyz.com HTTP/1.1
Some Headers


myvar1=value1&myvar2=value2&....

In a multipart message you receive the parameters in boundaries like the following

POST /rest/document/41 HTTP/1.1
Referer: http://localhost:8080/....
Accept-Language: de-at
Content-Type: multipart/form-data; boundary=---------------------------7d711d1020176
Host: localhost:8081
Content-Length: 393
Connection: Keep-Alive
Cache-Control: no-cache

-----------------------------7d711d1020176
Content-Disposition: form-data; name="Method"


POST
-----------------------------7d711d1020176
Content-Disposition: form-data; name="description"


Meine neue Version mit Veränderungen
-----------------------------7d711d1020176
Content-Disposition: form-data; name="version"


newMajorVersion
-----------------------------7d711d1020176--

You can use the MultipartParser from O'Reilly to parse the request.
Download the source code for the com.oreilly.servlet.multipart.MultipartParser from Koders at
com.oreilly.servlet.multipart.MultipartParser

make sure to create/download the additional classes too, search on Koders and you'll find.

:weed:
 
Joined
Mar 22, 2008
Messages
1
Reaction score
0
HTML File Upload using enctype=multipart/form-data in form

Should we use enctype=multipart/form-data in the form, I know it is recommended
if the form has <input type="file">. My form has <input type="file"> and other
html controls.

I tried the following,

form.html
=========
<FORM NAME="InputForm" ACTION="test.jsp" METHOD="POST" enctype=multipart/form-data>
<P><input type=text name="name" size=80>
<P><input type=file name="filename" size=80>
<P><input type="submit" value="Upload File Test">
</FORM>

test.jsp
=========
<p><%= "filename = " + request.getParameter("filename") %>
<p><%= "name = " + request.getParameter("name") %>


It will output
filename = null
name = null

I don't understand why, but if i remove enctype=multipart/form-data in the form,
then I am able to get the data.

Please advise. Thanks!!
 
Joined
Jul 7, 2008
Messages
1
Reaction score
0
licence

Hi,
I am a Web developer. I wished to use the O'Reilly's MultipartParser, but its licence is not free for the commercial projects and I am not supposed to use it. So please, do you see an another solution of making this?
 
Joined
Oct 19, 2009
Messages
1
Reaction score
0
Overlooked

I know everyone is searching for an elaborate solution here but if you just put the quotes in the proper spot in enctype=multipart/form-data it should work.

enctype="multipart/form-data"

Lava
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top