Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Problem in Uploading Excel File

Dear Friends,

I'm developing a new screen which will upload excel file from client to server system. Before uploading, i need to show the excel file data to the user in a table.

I designed the view(HCFileUploadDataView) with the following UIelements :

- FileUploadUI element - for selecting file.

- Retrieve button - onClick, will populate excel data in table.

- Table - display excel data to user.

- Upload button - for uploading file.

Coding part:


1). wdDoInit method :

public void wdDoInit()
  {
   	wdThis.wdGetFileUploadCustomControllerController().wdGetContext().currentContextElement().setRowCount(0);
	IWDAttributeInfo attributeInfo = wdContext.getNodeInfo().getAttribute(IPrivateHCFileUploadDataView.IContextElement.FILE_RESOURCE);
	ISimpleTypeModifiable type = attributeInfo.getModifiableSimpleType();
	IWDModifiableBinaryType binaryType = (IWDModifiableBinaryType)type;   
  }

2). Retrieve method :

public void onActionRetriveExcelData(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
  {
    //@@begin onActionRetriveExcelData(ServerEvent)
    Logger logger = LoggerFactory.getInstance().getLogger(HCFileUploadDataView.class);
		
    IPrivateHCFileUploadDataView.IContextElement element = null;	
    String fileName = null;	
    FileOutputStream fos = null;	
    ArrayList arrList = null;
    try 
    {
       element = wdContext.currentContextElement();		
       if(element.getFileResource() != null) 
       {
	fileName = wdContext.currentContextElement().getFileName();			
	logger.debug("input file name = " + fileName);
	if (!fileName.equals("") && !fileName.equals(null) && fileName.equalsIgnoreCase("input.xls")) 
	{
	   arrList = ExcelHCFileReader.loadDataGrid(new ByteArrayInputStream(element.getFileResource()));
	   logger.debug("arrList size = " + arrList.size());
	   if(arrList == null)
	   {
	      wdComponentAPI.getMessageManager().reportWarning("oops.. file was empty..");
	   } 
	   else 
	   {					
	      Collection hcFileUPloadTables = new ArrayList();				
	      Iterator itrHCTable     	   = arrList.iterator();	
	      HCFileUploadDO hcFileUploadDO = null;	
	      hcFileUploadDO = new HCFileUploadDO();	
	      int i = arrList.size();
          	      while(itrHCTable.hasNext()) 
                      {
	         IPrivateHCFileUploadDataView.IHCFileUploadTblElement tblElement = wdContext.createHCFileUploadTblElement();
	         hcFileUploadDO = (HCFileUploadDO)itrHCTable.next();
	         
                         // SEQUENCE_NO
	         String strSequenceNo = hcFileUploadDO.getStr_SequenceNo();
	         tblElement.setSequenceNo(strSequenceNo);
			
	          // AS_TO_DATE
	          String strAsToDate = hcFileUploadDO.getStr_AsToDate();
	          tblElement.setAsToDate(strAsToDate);
	
	          // HDR_ENT
	          String strHdrEnt = hcFileUploadDO.getStr_HdrEnt();
	          tblElement.setHdrEnt(strHdrEnt);
		
                          hcFileUPloadTables.add(tblElement);
	          wdContext.nodeHCFileUploadTbl().bind(hcFileUPloadTables);
   } 
   catch(Exception exception) 
   {
      wdComponentAPI.getMessageManager().reportException( exception.getMessage(), true );
      wdComponentAPI.getMessageManager().raisePendingException();		
   }
   element.setFileResource(null); 						

onclick of retrieve button the above action method(onActionRetriveExcelData) called, and i'm able to populate excel file data into table.


3). Upload method :

    public void onActionUploadFile(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
    {
    //@@begin onActionUploadFile(ServerEvent)
    
    Logger logger = LoggerFactory.getInstance().getLogger(HCFileUploadDataView.class);
        
    IPrivateHCFileUploadDataView.IContextElement element = null;
    FileOutputStream fos = null;
    String fileName = null;
    try 
    {
         element = wdContext.currentContextElement();
         if(element.getFileResource() != null) 
         {    	
	   byte fileArray[] = element.getFileResource();
	   fos = new FileOutputStream(new File("E:/input.xls"));
	   fos.write(fileArray);
	   fos.close();
	   wdComponentAPI.getMessageManager().reportSuccess("File Uploaded Successfully ");
         }    			
    } 
    catch(Exception exception) 
    {
       wdComponentAPI.getMessageManager().reportException( exception.getMessage(), true );
       wdComponentAPI.getMessageManager().raisePendingException();		
    }				
    //@@end
  }

Problem with above upload action method. I'm not able to upload the file. Because element.getFileResource() is coming as null. But element.getFileResource() is returning a value in retrieve method(onActionRetriveExcelData).

Could anyone help me how to resolve this ?

Thanks

Vijay.

Not what you were looking for? View more on this topic or Ask a question