Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   HTML File Upload using enctype=multipart/form-data in form? (http://www.velocityreviews.com/forums/t137597-html-file-upload-using-enctype-multipart-form-data-in-form.html)

Matt 10-12-2004 07:35 PM

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

Tim Slattery 10-12-2004 08:20 PM

Re: HTML File Upload using enctype=multipart/form-data in form?
 
jrefactors@hotmail.com (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
Slattery_T@bls.gov

Matt 10-13-2004 03:12 PM

Re: HTML File Upload using enctype=multipart/form-data in form?
 
Tim Slattery <Slattery_T@bls.gov> 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

Malcolm Dew-Jones 10-13-2004 08:04 PM

Re: HTML File Upload using enctype=multipart/form-data in form?
 
Matt (jrefactors@hotmail.com) wrote:
: Tim Slattery <Slattery_T@bls.gov> 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.

maxschremser 10-23-2007 10:13 AM

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:

viswa 03-22-2008 06:30 AM

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

mikeymike 07-07-2008 03:40 PM

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?

Lava 10-19-2009 09:38 PM

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

zeeshanakhter2009 05-26-2012 07:40 AM

i can solve like that
 
http://zeeshanakhter.com/2012/05/26/...partform-data/


using javascript .......................


All times are GMT. The time now is 12:12 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.