Home > Java, Web Dynpro > Web Dynpro – Download File

Web Dynpro – Download File

October 17th, 2009 Leave a comment Go to comments

Download file is a common requirement in custom web dynpro application. As we all know, in web dynpro we cannnot direct enter html code. Everything is hidden behind the model which designed using NWDS. Web dynpro provides an UI element named ‘FileDownload‘. Below is the steps for how to use FileDownload in web dynpro.

Step 1. Create needed value node and value attribute.

Create a new value node named ‘Resource’ and an value attribute named ‘resource’. The value attribute has the type of ‘com.sap.ide.webdynpro.uielementdefinitions.Resource‘. Then bind this attribute to the FileDownload UI element.

resource

Step 2. Initialize the the value attribute.

You need to initialize the resource attribute before showing the FileDownload element. In a common case, the file content is read from the SAP backend system, like ERP, Solution Manager, etc. A function module should be created in the backend system. The remote function module stores the file content in an internal table with type RAW. The ABAP dictionary type RAW will be mapped to java build-in type binary when import the RFC into a web dynpro model.

Below code is used to initialize the resource attribute.

    //get the file content from backend system
    wdThis.wdGetProj_evalController().executeZ_Get_Doc_Content_Input(classID , objID);
 
    //put all the file content in a byte array.
    byte[] binFile = new byte[0];
    for(int n = 0; n <; wdContext.nodeFile_Content_Binary().size(); n++){
        binFile = concat(binFile, wdContext.nodeFile_Content_Binary().getFile_Content_BinaryElementAt(n).getLine());     
    }     
    //initialize the resource attribute 	
    if(wdContext.nodeE_File_Attr().size() >; 0){
	String filename = wdContext.nodeE_File_Attr().currentE_File_AttrElement().getFile_Name();
	String fileext = "";
	if(filename.lastIndexOf(".") >; 0){
	    fileext = filename.substring(filename.lastIndexOf(".") + 1 , filename.length());
	    fileext = fileext.toUpperCase();
    }
 
    //reset the file name
    filename = wdContext.nodeEt_Saeval_Str().currentEt_Saeval_StrElement().getTitle_Dbl() + "." + fileext.toLowerCase();
    wdContext.nodeGlobal_Variant().currentGlobal_VariantElement().setFilename(filename);
 
    ByteArrayInputStream bais = new ByteArrayInputStream(binFile);
 
    WDWebResourceType resourceType = WDWebResourceType.getWebResourceTypeForFileExtension(
							fileext);
 
    if(resourceType != null){
    wdContext.nodeResource().currentResourceElement().setResource(
			WDResourceFactory.createResource(
					bais,
					filename,
					WDWebResourceType.getWebResourceTypeForFileExtension(
						fileext),
					false));
    }

After completing above code, the FileDownload can be used to download file.

  1. Lorenzo
    August 7th, 2011 at 02:38 | #1

    Great Tutorial! Exactly what i was waiting for. Thanks
    I suppose you can do the same with uploading in WD and then transfer to backend?

  2. August 7th, 2011 at 22:48 | #2

    Yes, Apache POI library can handle excel uploading.

  3. Lorenzo
    August 8th, 2011 at 20:49 | #3

    I am trying to use the code but concat method is missing :
    Could you share the “concat” method used in your code?
    Thanx ;-)

  4. August 15th, 2011 at 14:36 | #4

    This is not the standard API, it should be be defined by the develper. I don’t have the code now. It seems you need to define it by yourself :-)

  1. No trackbacks yet.