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

Discussion in 'Java' started by Matt, Oct 12, 2004.

  1. Matt

    Matt Guest

    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!!
     
    Matt, Oct 12, 2004
    #1
    1. Advertising

  2. Matt

    Tim Slattery Guest

    (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
     
    Tim Slattery, Oct 12, 2004
    #2
    1. Advertising

  3. Matt

    Matt Guest

    Tim Slattery <> 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
     
    Matt, Oct 13, 2004
    #3
  4. Matt () wrote:
    : Tim Slattery <> 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.
     
    Malcolm Dew-Jones, Oct 13, 2004
    #4
  5. Matt

    maxschremser

    Joined:
    Oct 23, 2007
    Messages:
    1
    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:
     
    maxschremser, Oct 23, 2007
    #5
  6. Matt

    viswa

    Joined:
    Mar 22, 2008
    Messages:
    1
    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!!
     
    viswa, Mar 22, 2008
    #6
  7. Matt

    mikeymike

    Joined:
    Jul 7, 2008
    Messages:
    1
    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?
     
    mikeymike, Jul 7, 2008
    #7
  8. Matt

    Lava

    Joined:
    Oct 19, 2009
    Messages:
    1
    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
     
    Lava, Oct 19, 2009
    #8
  9. Matt

    zeeshanakhter2009

    Joined:
    May 26, 2012
    Messages:
    1
    Location:
    Islamabad,Pakistan
    zeeshanakhter2009, May 26, 2012
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Duncan
    Replies:
    0
    Views:
    1,470
    Duncan
    Feb 24, 2006
  2. Matt
    Replies:
    1
    Views:
    11,199
    David Dorward
    Oct 12, 2004
  3. Replies:
    1
    Views:
    1,802
    Alexey Smirnov
    Apr 21, 2008
  4. David Lozzi

    Using <form ... enctype="multipart/form-data">

    David Lozzi, Aug 12, 2003, in forum: ASP General
    Replies:
    5
    Views:
    330
    Martin CLAVREUIL
    Aug 12, 2003
  5. Kevin DeValck
    Replies:
    1
    Views:
    740
    7stud --
    May 17, 2011
Loading...

Share This Page