Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Sam E
how to upload a file.
May 16, 2010 7:55 PM
Answer

Sam E

Rank: Liferay Master

Posts: 660

Join Date: March 2, 2009

Recent Posts

Hi,

I used the following java code in Action class to handle uploaded file:

Action class:
 1
 2
 3public void processAction(ActionMapping mapping, ActionForm form,
 4            PortletConfig portletConfig, ActionRequest actionRequest,
 5            ActionResponse actionResponse) throws Exception {
 6
 7        UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
 8                actionRequest);
 9
10        long imageId = ParamUtil.getLong(uploadRequest, "imageId");
11
12        long folderId = ParamUtil.getLong(uploadRequest, "folderId");
13        String name = ParamUtil.getString(uploadRequest, "name");
14        String fileName = uploadRequest.getFileName("file");
15        String uploadfileName = uploadRequest.getFileName("upload_file");
16        String description = ParamUtil.getString(
17                uploadRequest, "description", fileName);
18
19        File file = uploadRequest.getFile("file");
20        String contentType = getContentType(uploadRequest, file);
21
22        if (contentType.equals("application/octet-stream")) {
23                String ext = GetterUtil.getString(
24                        FileUtil.getExtension(file.getName())).toLowerCase();
25
26                if (Validator.isNotNull(ext)) {
27                        contentType = ContentTypeUtil.getContentType(ext);
28                }
29        }


jsp file:
 1
 2<tr>
 3    <td class="lfr-label" valign="top">
 4        <liferay-ui:message key="upload-product-photos" />
 5    </td>
 6    <td>
 7        <input id="my_file_element" type="file" name="<portlet:namespace />upload_file" >
 8        <!-- This is where the output will appear -->
 9        <div id="files_list"></div>
10    </td>
11</tr>
12
13
14<script>
15    <!-- Create an instance of the multiSelector class, pass it the output target and the max number of files -->
16    var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 20 );
17    <!-- Pass in the file element -->
18    multi_selector.addElement( document.getElementById( 'my_file_element' ) );
19</script>


The following error is thrown when I pressed the Submit button:

 1
 201:46:06,888 ERROR [UploadServletRequestImpl:111] org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded
 3org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded
 4        at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:885)
 5        at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
 6        at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:349)
 7        at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
 8        at com.liferay.portal.upload.LiferayFileUpload.parseRequest(LiferayFileUpload.java:67)
 9        at com.liferay.portal.upload.UploadServletRequestImpl.<init>(UploadServletRequestImpl.java:82)
10        at com.liferay.portal.util.PortalImpl.getUploadServletRequest(PortalImpl.java:2426)
11....
12...


What is the correct coding for handling upload file?
Very appreciated for any suggestion.

Sam
Babu Janarthanan Ramachandran
RE: how to upload a file.
May 16, 2010 9:15 PM
Answer

Babu Janarthanan Ramachandran

Rank: Regular Member

Posts: 132

Join Date: July 30, 2008

Recent Posts

Hi,
It seems that you are missing the enctype.

your JSP should contain

enctype="multipart/form-data" method="post"

hope it helps you...

Thx,
Babu
Sam E
RE: how to upload a file.
May 16, 2010 11:07 PM
Answer

Sam E

Rank: Liferay Master

Posts: 660

Join Date: March 2, 2009

Recent Posts

Babu Janarthanan:
Hi,
It seems that you are missing the enctype.

your JSP should contain

enctype="multipart/form-data" method="post"

hope it helps you...

Thx,
Babu


Thanks Babu, that fixed the error.

Do you know how to pass the uploaded file information to the Action class?

At the moment, I use the following code to retrieve file info in the Action class:

1
2public void processAction(ActionMapping mapping, ActionForm form,
3            PortletConfig portletConfig, ActionRequest actionRequest,
4            ActionResponse actionResponse) throws Exception {
5 String uploadfileName = uploadRequest.getFileName("upload_file");
6
7...


in JSP:

1
2<input id="my_file_element" type="file" name="<portlet:namespace />upload_file" >
3        <!-- This is where the output will appear -->
4        <div id="files_list"></div>


But uploadfileName returns null.

What have I done wrong?


Thanks
Sam
Babu Janarthanan Ramachandran
RE: how to upload a file.
May 16, 2010 11:32 PM
Answer

Babu Janarthanan Ramachandran

Rank: Regular Member

Posts: 132

Join Date: July 30, 2008

Recent Posts

Hi,
You can use the below mentioned code snippet.

UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest( actionRequest);

from uploadRequest you can get the details like FileName, FullFileName, ContentType, etc.

take a look at UploadPortletRequest.java, which is available in the portal source.

hope it helps you....

Thx,
Babu
Sipin Verma
how to set enctype in A io request call
December 14, 2010 5:44 AM
Answer

Sipin Verma

Rank: New Member

Posts: 23

Join Date: December 14, 2010

Recent Posts

Hi, I am making a jsp page that uploads text as well an image file.
here is excerpt of my form declaration:

<aui:form action="<%= editPageActionURL %>" enctype="multipart/form-data" method="post" name="fm" onSubmit='<%= "event.preventDefault();" %>'>
...........

Then I have craeted a method to upload the data to java action class with struts action.
And this method contains A.io.request ajax call, because I dont to submit the form. Here is the alloy ui request:

A.io.request(url,
{
method: 'POST',
enctype : 'multipart/form-data',
data: {...
......

But when I run my code I get below error

18:34:16,390 ERROR [UploadServletRequestImpl:101] org.apache.commons.fileupload.
FileUploadBase$InvalidContentTypeException: the request doesn't contain a multipart/form-data or
multipart/mixed stream, content type header is application/x-www-form-urlencoded; charset=UTF-8
org.apache.commons.fileupload.FileUploadBase$InvalidContentTypeException: the request doesn't contain a
multipart/form-data or multipart/mixed stream, content type header is application/x-www-form-urlencoded;
charset=UTF-8

I suspect the way I am setting the enctype in A.io.request is wrong.
Need your help... Looking forward for your replies.
ThankUAll...
Jan Tošovský
RE: how to set enctype in A io request call
February 25, 2011 6:59 AM
Answer

Jan Tošovský

Rank: Expert

Posts: 424

Join Date: July 22, 2010

Recent Posts

Hi Sipin,

I use slightly different syntax, but it ends with the same content-type exception.

<aui:form action="<%= actionURL %>" method="post" name="fm" enctype="multipart/mixed">

I use 'multipart/mixed' now according to the hint at http://www.liferay.com/community/forums/-/message_boards/message/7142241/maximized

Any progress in solving this issue?

Regards,
Jan
zahra zarei
RE: how to upload a file.
September 11, 2011 8:48 PM
Answer

zahra zarei

Rank: New Member

Posts: 9

Join Date: August 26, 2011

Recent Posts

hi guys,
I have a form with some text fields and a file field when I use enctype="multipart/form-data" I can't get text feilds from request,

<aui:form name="fm" enctype="multipart/mixed" action="<%= updateProductURL.toString() %>"
method="post">

<aui:fieldset>

<aui:model-context bean="<%= product %>" model="<%= Product.class %>" />

<aui:input name="product Id" type="hidden" />

<aui:input name="product Text" first="true" autoFocus="true" size="45" />

<aui:input name="serialNumber" size="45" />

<aui:input type="textarea" name="productDesc" size="75"></aui:input>

<aui:input type="textarea" name="technicalDesc" size="75"></aui:input>

<aui:input type="file" name="pic" size="75"/>

and in my code

product .setproduct Text(ParamUtil.getString(request, "product Text"));
product .setProductDesc(ParamUtil.getString(request, "productDesc"));
product .setTechnicalDesc(ParamUtil.getString(request, "technicalDesc"));
product .setSerialNumber(ParamUtil.getString(request, "serialNumber"));
UploadPortletRequest uploadRequest=PortalUtil.getUploadPortletRequest(request);
File f=uploadRequest.getFile("pic");
byte[] bytes = null;
if (f != null) {
bytes = FileUtil.getBytes(f);
}
String pic=Base64.objectToString(bytes);
product .setPic(pic);

can any one tell me what's wrong please?

thanks
Sipin Verma
RE: how to upload a file.
September 11, 2011 10:09 PM
Answer

Sipin Verma

Rank: New Member

Posts: 23

Join Date: December 14, 2010

Recent Posts

Zahra you are using enctype="multipart/mixed"; try using enctype="multipart/form-data"

Also if it doesn't works by changign the enctype then try using simple form tags instead of aui tags and see if things works for you...I did like this and its working absolutely fine for me:
in Jsp:
<form name="fm" action="<portlet:actionURL name="someAction"/>" method="post" enctype="multipart/form-data" >
<input type="file" name="FileName" id="FileName" size="40" />
<input type="submit" value="Upload File"/>
</form>

In Action method of Portlet java file:
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(actionRequest);
File file = uploadRequest.getFile("FileName");
// Now process the file with your code
Manikandan S
RE: how to upload a file.
September 14, 2011 2:25 AM
Answer

Manikandan S

Rank: Expert

Posts: 492

Join Date: September 15, 2010

Recent Posts

You can see this thread, which is used for custom portlet.
Ajeykumar Hulivana Boregowda
RE: how to upload a file.
April 14, 2012 12:03 AM
Answer

Ajeykumar Hulivana Boregowda

Rank: Junior Member

Posts: 72

Join Date: March 12, 2012

Recent Posts

The form field values can be retrieved using the following code....
Steps
1. create object to the UploadPortletRequest
UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);
2. if you want other form field data you should use bellow code
String category=uploadRequest.getParameter("name");
Nrupay N Shah
RE: how to set enctype in A io request call
November 17, 2012 12:00 AM
Answer

Nrupay N Shah

Rank: New Member

Posts: 19

Join Date: December 10, 2009

Recent Posts

Hello Sipin,

You are getting this issue because you are using Ajax call to submit the form which contains files.
Now when you are calling this AUI script to submit the form, its serializing the form content and not setting the form header/enctype to multipart/mixed or multipart/form-data even you had set it.

You have to pass the parameter upload:true in aui script.

A.io.request(url,
{
method: 'POST',
enctype : 'multipart/form-data',
upload : true,
data: {...
......

try this solution , I am sure that your problem will be resolved by using it.

care n joy
Nrupay Shah
Harsha Mhaske
RE: how to set enctype in A io request call
June 20, 2013 5:54 AM
Answer

Harsha Mhaske

Rank: New Member

Posts: 15

Join Date: September 26, 2008

Recent Posts

Nrupay N Shah:
Hello Sipin,

You are getting this issue because you are using Ajax call to submit the form which contains files.
Now when you are calling this AUI script to submit the form, its serializing the form content and not setting the form header/enctype to multipart/mixed or multipart/form-data even you had set it.

You have to pass the parameter upload:true in aui script.

A.io.request(url,
{
method: 'POST',
enctype : 'multipart/form-data',
upload : true,
data: {...
......

try this solution , I am sure that your problem will be resolved by using it.

care n joy
Nrupay Shah


This is not working. Any solutions?

Regards,
Harsha
Cristiano Vavala
RE: how to set enctype in A io request call
July 27, 2015 3:44 AM
Answer

Cristiano Vavala

Rank: New Member

Posts: 1

Join Date: April 9, 2015

Recent Posts

This works for me, setting upload: true inside the form attribute:

 1    A.io.request(
 2        url,
 3        {
 4            method: 'POST',
 5            form: {
 6                 id: '<portlet:namespace/>fm',
 7                 upload: true}
 8            }
 9        }
10    );