Skip to Content
avatar image
Former Member

Download file from KM

Hi Experts,

I need to develop a webdynpro application that would download a file from KM can u please help out by providing sample code

Thanking you

Regards

Noel

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • avatar image
    Former Member
    Oct 11, 2007 at 06:40 AM

    chk it

    /people/bobu.georgeputheeckal/blog/2006/12/22/getting-an-image-from-km-documents-to-be-used-in-web-dynpro

    public void DownloadFile( )

    {

    //@@begin DownloadFile()

    IWDMessageManager msgMgr = wdThis.wdGetAPI().getComponent().getMessageManager();

    String strAppraisalID = "";

    RID objRid = null;

    RID objNewRid = null;

    RID objRid1 = null;

    RID objNewRid1 = null;

    RID objNewRid2 = null;

    RID objRid2 = null;

    RID objNewRid3 = null;

    IResourceFactory objFactory = null;

    String strLocation = "";

    String strPath = "/documents";

    ICollection objRootFolder = null;

    ICollection objAUTFolder = null;

    ICollection objAppraisalFolder = null;

    ICollection objAppraisalFolderActual = null;

    ICollection objRowwiseFolder = null;

    ICollection objRowwiseFolderActual = null;

    ResourceContext objResourceContext = null;

    String strFileName = "";

    File objFile = null;

    ByteArrayOutputStream objFileOutputStream = null;

    InputStream objFileInputStream = null;

    IResource objFileResource = null;

    IResourceList objResourceList = null;

    IResource objResource1 = null;

    IResource objResourceCheckRowwiseFolder = null;

    IResource objResourceCheckFolderAUT = null;

    ICollection objResourceCheckFolderAppraisal = null;

    IResourceListIterator objResourceListIterator = null;

    IResource objTempResource = null;

    IContent objContent = null;

    IWDClientUser objUser = null;

    com.sap.security.api.IUser objSAPUser = null;

    IUser ep5User = null;

    int intRowIndex = 0;

    int intSizeOfFolder = 0;

    URI objURI = null;

    IHierarchicalUri objURIReference = null;

    InputStream objInputStream = null;

    try

    {

    strAppraisalID = wdContext.currentContextElement().getCtx_AppraisalID();

    intRowIndex = wdContext.currentContextElement().getCtx_RowIndex();

    strLocation = strPath"/""AUT""/"strAppraisalID"/"intRowIndex;

    objNewRid2 = RID.getRID(strLocation);

    objUser = WDClientUser.getCurrentUser();

    objSAPUser = objUser.getSAPUser();

    ep5User = WPUMFactory.getUserFactory().getEP5User(objSAPUser);

    objResourceContext = new ResourceContext(ep5User);

    objFactory = ResourceFactory.getInstance();

    objResourceCheckFolderAppraisal = (ICollection)objFactory.getResource(objNewRid2,objResourceContext);

    if (objResourceCheckFolderAppraisal!=null)

    {

    objResourceList = objResourceCheckFolderAppraisal.getChildren();

    }

    if (objResourceList!=null)

    {

    objResourceListIterator = objResourceList.listIterator();

    while (objResourceListIterator.hasNext())

    {

    //binaryType.setMimeType(WDWebResourceType.XLS);

    objTempResource = objResourceListIterator.next();

    strFileName = objTempResource.getDisplayName();

    //objURI = objTempResource.getAccessURI();

    //msgMgr.reportSuccess("URI OBJECT "+objURI.toString());

    if((strFileName==null) || (strFileName.length()==0))

    {

    strFileName = objTempResource.getName();

    if((strFileName!=null) && (strFileName.length()!=0))

    {

    if (strFileName.toLowerCase().endsWith(".doc"))

    {

    binaryType.setMimeType(WDWebResourceType.DOC);

    }

    else if ((strFileName.toLowerCase().endsWith(".xls")) ||

    (strFileName.toLowerCase().endsWith(".xlt")))

    {

    binaryType.setMimeType(WDWebResourceType.XLS);

    }

    else if (strFileName.toLowerCase().endsWith(".pdf"))

    {

    binaryType.setMimeType(WDWebResourceType.PDF);

    }

    else if (strFileName.toLowerCase().endsWith(".ppt"))

    {

    binaryType.setMimeType(WDWebResourceType.PPT);

    }

    else if (strFileName.toLowerCase().endsWith(".gif"))

    {

    binaryType.setMimeType(WDWebResourceType.GIF_IMAGE);

    }

    else if (strFileName.toLowerCase().endsWith(".jpg"))

    {

    binaryType.setMimeType(WDWebResourceType.JPG_IMAGE);

    }

    else

    {

    binaryType.setMimeType(WDWebResourceType.TXT);

    }

    }

    msgMgr.reportSuccess("MIME DOWNLOAD "+binaryType.getMimeType().getHtmlMime());

    objContent = objTempResource.getContent();

    objInputStream = objContent.getInputStream();//

    int intInputDataLength = objInputStream.available();

    msgMgr.reportSuccess("BYTE Data SIZE while Downloading::"+intInputDataLength);

    wdContext.currentContextElement().setCtx_FileResource(

    this.getByteArrayFromResourcePath(objInputStream));

    binaryType.setFileName(strFileName);

    msgMgr.reportSuccess("File Name while Downloading::"+strFileName);

    }

    }

    }

    }

    catch(Exception e)

    {

    //Getting the message

    String msg = e.getLocalizedMessage();

    //Checking for null values and zero length of message

    if ((msg==null) || (msg.length()==0))

    {

    msg = e.getMessage();

    }//end of if ((msg==null) || (msg.length()==0))

    if ((msg == null) || (msg.length() == 0))

    {

    //Converting the message into string

    msg = e.toString();

    }//end of if ((msg==null) || (msg.length()==0))

    //Reporting the exception

    msgMgr.reportException(msg,true);

    }//end of catch

    //@@end

    }

    private byte[] getByteArrayFromResourcePath(InputStream in)

    throws FileNotFoundException, IOException {

    ByteArrayOutputStream out = new ByteArrayOutputStream();

    //FileOutputStream out = new FileOutputStream();

    int intInputDataLength = 0;

    byte[] part = new byte[in.available()];

    int nextByte = in.read(part);

    in.close();

    out.write(part);

    return out.toByteArray();

    }

    Add comment
    10|10000 characters needed characters exceeded

    • Former Member Former Member

      Hi Noel,

      Use the LinkToUrl UI Element.

      intCounter parameter is not required.

      Call the DownloadFile anywhere in the view controller preferably in the onPlugXXX method.

      You need to have two root level context attributes ctx_URL and ctx_URLText of type string .

      Bind the ctx_URL attribute to the REFERENCE property of the LinkToUrl UI Element and the ctx_URLText attribute to the TEXT property of LinkToUrl UI Element.

      This should work..

      Regards,

      Tahzeeb.

      Message was edited by:

      Armin Reichert