Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does form with enctype="multipart/form-data" cause problems accessing a hidden field

I have created a hidden form element

<form name="UploadImage" enctype="multipart/form-data" method="post" action="UploadImage">
    <label>
        </label>
    <input name="imgUploadObjId" id="imgUploadObjId" value="52" type="hidden">

    //rest of the form here

</form>

And I am trying to get the value with this line in a servlet (as I have done before):

int objId = Integer.parseInt(request.getParameter("imgUploadObjId"));

But I get this (line 33 is the line above):

java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Unknown Source) java.lang.Integer.parseInt(Unknown Source) web.objects.UploadImage.doPost(UploadImage.java:33) javax.servlet.http.HttpServlet.service(HttpServlet.java:637) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

Is there something different about a form with enctype="multipart/form-data"? Or can you see some other error.

like image 402
Ankur Avatar asked May 13 '10 14:05

Ankur


People also ask

What does Enctype multipart form data do?

Multipart form data: The ENCTYPE attribute of <form> tag specifies the method of encoding for the form data. It is one of the two ways of encoding the HTML form. It is specifically used when file uploading is required in HTML form. It sends the form data to server in multiple parts because of large size of file.

What is the purpose of Enctype in form?

enctype property is the MIME type of content that is used to submit the form to the server. Possible values are: application/x-www-form-urlencoded : The initial default type. multipart/form-data : The type that allows file <input> element(s) to upload file data.

What is Enctype multipart form data in MVC?

The enctype = 'multipart/form-data' attribute is required when the Form is used for uploading Files using HTML FileUpload element. Download Code Sample. Download Free Word/PDF/Excel API. In this article I will explain with an example, how to set enctype = 'multipart/form-data' attribute for Form Tag with Html.

Is multipart form data safe?

Our security department, recently, informed us we should stop and avoid to use the content type 'multipart/form-data' in order to send a file to a web server, because it is not considered 'safe'.


2 Answers

The servlet parses the parameters by default using application/x-www-form-urlencoded encoding. The multipart/form-data encoding however isn't supported in servlets until Servlet 3.0. The getParameter() calls will all return null.

In Servlet 3.0, you should have used HttpServletRequest#getParts() instead to get all parts of a multipart/form-data request, including normal form fields. Prior to Servlet 3.0, you should have used Apache Commons FileUpload to parse multipart/form-data requests. See also the following answer for a detailed example of both approaches: How to upload files to server using JSP/Servlet?

Note that if you aren't using any <input type="file"> field at all, then you can just leave the encoding away from the <form>. It will then default to application/x-www-form-urlencoded.

like image 112
BalusC Avatar answered Sep 20 '22 23:09

BalusC


As a workaround, you can also add the required hidden parameters as GET parameters in the form's action attribute:

<form name="UploadImage" enctype="multipart/form-data" method="post" action="UploadImage?imgUploadObjId=52">

    //rest of the form here

</form>

this will allow the request.getParameter("imgUploadObjId") call to work.

like image 26
Gabriel Belingueres Avatar answered Sep 21 '22 23:09

Gabriel Belingueres