Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > HTML File Upload using enctype=multipart/form-data in form?

Reply
Thread Tools

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

 
 
Matt
Guest
Posts: n/a
 
      10-12-2004
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!!
 
Reply With Quote
 
 
 
 
Tim Slattery
Guest
Posts: n/a
 
      10-12-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Matt) wrote:

>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.


If your form include an <input type="file"...> element, then you
*must* user enctype="multipart/form-data".

>
>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.

--
Tim Slattery
(E-Mail Removed)
 
Reply With Quote
 
 
 
 
Matt
Guest
Posts: n/a
 
      10-13-2004
Tim Slattery <(E-Mail Removed)> wrote in message

> 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
 
Reply With Quote
 
Malcolm Dew-Jones
Guest
Posts: n/a
 
      10-13-2004
Matt ((E-Mail Removed)) wrote:
: Tim Slattery <(E-Mail Removed)> wrote in message

: > 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.
 
Reply With Quote
 
maxschremser maxschremser is offline
Junior Member
Join Date: Oct 2007
Posts: 1
 
      10-23-2007
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.

 
Reply With Quote
 
viswa viswa is offline
Junior Member
Join Date: Mar 2008
Posts: 1
 
      03-22-2008
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!!
 
Reply With Quote
 
mikeymike mikeymike is offline
Junior Member
Join Date: Jul 2008
Posts: 1
 
      07-07-2008
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?
 
Reply With Quote
 
Lava Lava is offline
Junior Member
Join Date: Oct 2009
Posts: 1
 
      10-19-2009
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
 
Reply With Quote
 
zeeshanakhter2009 zeeshanakhter2009 is offline
Junior Member
Join Date: May 2012
Location: Islamabad,Pakistan
Posts: 1
 
      05-26-2012
http://zeeshanakhter.com/2012/05/26/...partform-data/


using javascript .......................
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File upload from client application (non-form based upload) stuart@microsoft.com Python 1 11-25-2006 12:14 AM
Upload a file without file Upload control - ASP.Net =?Utf-8?B?U2FyYXY=?= ASP .Net 3 08-03-2005 01:09 AM
HTML File Upload using enctype=multipart/form-data in form? Matt HTML 1 10-12-2004 08:17 PM
How to detect sizeof upload in order to show status of browser file upload? Heather Fraser Java 0 07-05-2004 12:35 AM
Upload Excel with Macros using HTML File Upload Prakash ASP General 3 11-12-2003 04:26 AM



Advertisments