Fórum

how to upload a file.

Sam Wan, modificado 13 Anos atrás.

how to upload a file.

Liferay Master Postagens: 660 Data de Entrada: 03/03/09 Postagens Recentes
Hi,

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

Action class:


public void processAction(ActionMapping mapping, ActionForm form,
			PortletConfig portletConfig, ActionRequest actionRequest,
			ActionResponse actionResponse) throws Exception {

		UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(
                actionRequest);

        long imageId = ParamUtil.getLong(uploadRequest, "imageId");

        long folderId = ParamUtil.getLong(uploadRequest, "folderId");
        String name = ParamUtil.getString(uploadRequest, "name");
        String fileName = uploadRequest.getFileName("file");
        String uploadfileName = uploadRequest.getFileName("upload_file");
        String description = ParamUtil.getString(
                uploadRequest, "description", fileName);

        File file = uploadRequest.getFile("file");
        String contentType = getContentType(uploadRequest, file);

        if (contentType.equals("application/octet-stream")) {
                String ext = GetterUtil.getString(
                        FileUtil.getExtension(file.getName())).toLowerCase();

                if (Validator.isNotNull(ext)) {
                        contentType = ContentTypeUtil.getContentType(ext);
                }
        }



jsp file:


	
		<liferay-ui:message key="upload-product-photos" />
	
	
		<input id="my_file_element" type="file" name="<portlet:namespace />upload_file">
		<!-- This is where the output will appear -->
		<div id="files_list"></div>
	



<script>
	<!-- Create an instance of the multiSelector class, pass it the output target and the max number of files -->
	var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 20 );
	<!-- Pass in the file element -->
	multi_selector.addElement( document.getElementById( 'my_file_element' ) );
</script>


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


01: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
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
        at org.apache.commons.fileupload.FileUploadBase$FileItemIteratorImpl.<init>(FileUploadBase.java:885)
        at org.apache.commons.fileupload.FileUploadBase.getItemIterator(FileUploadBase.java:331)
        at org.apache.commons.fileupload.FileUploadBase.parseRequest(FileUploadBase.java:349)
        at org.apache.commons.fileupload.servlet.ServletFileUpload.parseRequest(ServletFileUpload.java:126)
        at com.liferay.portal.upload.LiferayFileUpload.parseRequest(LiferayFileUpload.java:67)
        at com.liferay.portal.upload.UploadServletRequestImpl.<init>(UploadServletRequestImpl.java:82)
        at com.liferay.portal.util.PortalImpl.getUploadServletRequest(PortalImpl.java:2426)
....
...

</init></init>


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

Sam
Babu Janarthanan, modificado 13 Anos atrás.

RE: how to upload a file.

Regular Member Postagens: 128 Data de Entrada: 31/07/08 Postagens Recentes
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 Wan, modificado 13 Anos atrás.

RE: how to upload a file.

Liferay Master Postagens: 660 Data de Entrada: 03/03/09 Postagens Recentes
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:


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

...


in JSP:


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


But uploadfileName returns null.

What have I done wrong?


Thanks
Sam
Babu Janarthanan, modificado 13 Anos atrás.

RE: how to upload a file.

Regular Member Postagens: 128 Data de Entrada: 31/07/08 Postagens Recentes
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
thumbnail
sipin verma, modificado 13 Anos atrás.

how to set enctype in A io request call

Junior Member Postagens: 29 Data de Entrada: 14/12/10 Postagens Recentes
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ý, modificado 13 Anos atrás.

RE: how to set enctype in A io request call

Liferay Master Postagens: 566 Data de Entrada: 22/07/10 Postagens Recentes
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
Nrupay N Shah, modificado 11 Anos atrás.

RE: how to set enctype in A io request call

New Member Postagens: 19 Data de Entrada: 10/12/09 Postagens Recentes
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, modificado 10 Anos atrás.

RE: how to set enctype in A io request call

New Member Postagens: 15 Data de Entrada: 26/09/08 Postagens Recentes
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, modificado 8 Anos atrás.

RE: how to set enctype in A io request call

New Member Mensagem: 1 Data de Entrada: 09/04/15 Postagens Recentes
This works for me, setting upload: true inside the form attribute:

    A.io.request(
        url,
        {
            method: 'POST',
            form: {
                 id: '<portlet:namespace />fm', 
                 upload: true}
            }
        }
    );
zahra zarei, modificado 12 Anos atrás.

RE: how to upload a file.

New Member Postagens: 9 Data de Entrada: 26/08/11 Postagens Recentes
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
thumbnail
Sipin Verma, modificado 12 Anos atrás.

RE: how to upload a file.

Junior Member Postagens: 29 Data de Entrada: 14/12/10 Postagens Recentes
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
thumbnail
Mani kandan, modificado 12 Anos atrás.

RE: how to upload a file.

Expert Postagens: 492 Data de Entrada: 15/09/10 Postagens Recentes
You can see this thread, which is used for custom portlet.
thumbnail
Ajeykumar HB, modificado 12 Anos atrás.

RE: how to upload a file.

Junior Member Postagens: 72 Data de Entrada: 13/03/12 Postagens Recentes
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");