Hello Swati,

You already have all in ofbiz to load natively. Check on the source code the service uploadPartyContentFile.

    <service name="uploadPartyContentFile" engine="group" transaction-timeout="300">
        <description>Upload and attach a file to a party</description>
        <group>
            <invoke name="createContentFromUploadedFile" result-to-context="true"/>
            <invoke name="createPartyContent"/>
        </group>
    </service>

You create you own service who load the file through createContentFromUploadedFile and after call your specific working process on second service.

After for the form ckeck all form with '<form name="AddPartyContent" type="upload" ' with a attribute of type <file>

Nicolas

On 18/05/2023 00:54, Swati Srivastava wrote:
Hello Community.



I am trying to upload a file (image/txt/word/pdf) in the ofbiz application 
using ftl and java method. After parsing the file , item listed in the list is 
coming as null. Please help.



My FTL code is a simple form like below with name testUpload.ft

​

<form action="uploadFile" method="post" enctype="multipart/form-data">

     <input type="file" name="file" />

     <input type="submit" value="Upload" />

</form>



My controller is configured as follows:



<request-map uri="fileUplodingFtl">

<security https="true" auth="true"/>

<response name="success" type="view" value="fileUplodingFtl"/>

</request-map>

<view-map name="fileUplodingFtl" type="screen" 
page="component://xerus/widget/XerusScreens.xml#fileUplodingFtl"/>



<request-map uri="uploadFile">

     <security https="true" auth="true"/>

     <event type="java" path="com.apache.xerus.services.FileUpload" 
invoke="uploadFile"/>

     <response name="success" type="view" value="fileUplodingFtl"/>

</request-map>



In the java class FileUpload.java:



package com.apache.xerus.services;



import org.apache.commons.fileupload.FileItem;

import org.apache.commons.fileupload.FileUploadException;

import org.apache.commons.fileupload.disk.DiskFileItemFactory;

import org.apache.commons.fileupload.servlet.ServletFileUpload;

import java.io.File;

import java.nio.ByteBuffer;

import java.util.HashMap;

import java.util.List;

import java.util.Locale;

import java.util.Map;

import javax.servlet.ServletConfig;

import javax.servlet.ServletContext;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import java.io.IOException;

import java.net.URLEncoder;

import java.util.Locale;

import org.apache.commons.io.FilenameUtils;

import java.io.InputStream;

import java.io.OutputStream;

import java.io.OutputStreamWriter;

import java.io.Writer;

import java.net.HttpURLConnection;

import java.net.URL;

import org.apache.ofbiz.base.util.Debug;

import org.apache.ofbiz.base.util.GeneralException;

import org.apache.ofbiz.base.util.GeneralRuntimeException;

import java.util.Iterator;

import org.apache.ofbiz.entity.Delegator;

import org.apache.ofbiz.entity.GenericEntityException;

import org.apache.ofbiz.entity.GenericValue;

import org.apache.ofbiz.service.LocalDispatcher;

import org.apache.ofbiz.base.util.UtilGenerics;

import org.apache.ofbiz.base.util.UtilHttp;

import javax.servlet.http.HttpSession;

import org.apache.ofbiz.base.util.FileUtil;

import org.apache.ofbiz.base.util.UtilProperties;

import org.apache.ofbiz.service.ServiceUtil;





public class FileUpload {



     public static final String module = FileUpload.class.getName();

     public static String uploadFile(HttpServletRequest request, 
HttpServletResponse response) {

   // Check if the request is a multipart request

         boolean isMultipart = ServletFileUpload.isMultipartContent(request);



         if (isMultipart) {

             // Create a file upload handler

             DiskFileItemFactory factory = new DiskFileItemFactory();

             ServletFileUpload upload = new ServletFileUpload(factory);



             try {

                 // Parse the request

                 List<FileItem> items = upload.parseRequest(request);

                 System.out.println(" Items present : "+items);

                 // Process each uploaded file

                 for (FileItem item : items) {

                     if (!item.isFormField()) {

                         // Get the uploaded file name

                         String fileName = item.getName();

                         System.out.println("fileName : "+fileName);

                         // Get the uploaded file content

                         byte[] fileContent = item.get();

                         System.out.println("fileContent : "+fileContent);

                         // Process the file as needed (e.g., save to disk, 
store in database, etc.)

                         // ...

                     }

                 }

             } catch (FileUploadException e) {

                 Debug.logError(e, module);

                 Debug.logError("[FileUpload.FileUpload] " + e.getMessage(), 
module);

             }

         } else {

             System.out.println("It is not multipart");

         }





I have added the screen in XerusSceen.xml and logging in the app by typing 
localhost:8443/xerus/control/fileUplodingFtl and the page is opening but when I 
am selecting the file and pressing the Upload button, I can see in the logs of 
ofbiz  as  ' Items present: {} '. I am wondering , what is wrong with this 
code? Why is there no file present



Please guide me how to achieve uploading of files in OFbiz



Thank You



Regards,

Swati

Reply via email to