on 10-11-2007 7:28 AM
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
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();
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Noel,
The code pasted by Kanwalpreet <b>will not work</b>..I know it because <b>I have written this code</b>...KANWALPREET , next time please make sure that the code is working before copying and pasting it here so that you <b>don't mislead others</b>.
The working code is as follows:
public void DownloadFile( int intCounter )
{
//@@begin DownloadFile()
//Getting the message manager
IWDMessageManager msgMgr = wdThis.wdGetAPI().getComponent().getMessageManager();
String strAppraisalID = "";
String strLocation = "";
String strPath = "/documents";
String strUrl = "";
String strUserID = "";
String strFileName = "";
String strLevel = "";
RID objNewRid2 = null;
ResourceContext objResourceContext = null;
IResourceFactory objFactory = null;
IResourceList objResourceList = null;
ICollection objResourceCheckFolderAppraisal = null;
IResourceListIterator objResourceListIterator = null;
IResource objTempResource = null;
IWDClientUser objUser = null;
IURLGeneratorService objService = null;
IUser ep5User = null;
IUriReference objUriReference = null;
com.sap.security.api.IUser objSAPUser = null;
try
{
strLevel = wdContext.currentContextElement().getCtx_Level();
// //Getting the appraisal id of the employee
strAppraisalID = wdContext.currentContextElement().getCtx_AppraisalID();
//Getting the current user
objUser = WDClientUser.getCurrentUser();
//Getting the SAP user
objSAPUser = objUser.getSAPUser();
//Getting the EP user
ep5User = WPUMFactory.getUserFactory().getEP5User(objSAPUser);
//Getting the resourse context of the EP user
objResourceContext = new ResourceContext(ep5User);
//Getting an instance of the resource factory
objFactory = ResourceFactory.getInstance();
//Getting the final location or destination
strLocation = strPath+"/"+"AUT";
//Getting the resource ID of the final location
objNewRid2 = RID.getRID(strLocation);
//Getting the collection of resource of final location
objResourceCheckFolderAppraisal = (ICollection)objFactory.getResource(objNewRid2,objResourceContext);
//Checking if the resource is not null
if (objResourceCheckFolderAppraisal!=null)
{
//Getting children of that resource into a resource list
objResourceList = objResourceCheckFolderAppraisal.getChildren();
}//if (objResourceCheckFolderAppraisal!=null)
//Checking if the resourcelist is not null
if (objResourceList!=null)
{
//Obtaining an iterator object on this resource list
objResourceListIterator = objResourceList.listIterator();
//Looping till the resource list has elements
while (objResourceListIterator.hasNext())
{
//Getting next element into a temporary object
objTempResource = objResourceListIterator.next();
strFileName = objTempResource.getName();
//Generating the URL service
objService = (IURLGeneratorService)ResourceFactory.getInstance().getServiceFactory().getService(IServiceTypesConst.URLGENERATOR_SERVICE);
//Getting the relative URIReference from the service object
objUriReference = objService.getRelativeUriPathKey.CONTENT_ACCESS_PATH).appendPath(objTempResource.getRID().toExternalForm());
//Getting the URL by converting the URIReference to string
strUrl = objUriReference.toString();
//Setting this URL to context attribute that is bound to the REFERENCE property of LInkToURL UI Element
wdContext.currentContextElement.setCtx_URL(strUrl);
//Setting this string to context attribute that is bound to the TEXT property of LInkToURL UI Element
wdContext.currentContextElement.setCtx_URLText(strFileName);
)
}//end of while (objResourceListIterator.hasNext())
}//end of if (objResourceList!=null)
}//end of try
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
finally
{
//Setting interface reference variables to Null
strAppraisalID = null;
objNewRid2 = null;
objFactory = null;
strLocation = null;
strPath = null;
objResourceContext = null;
objResourceList = null;
objResourceCheckFolderAppraisal = null;
objResourceListIterator = null;
objTempResource = null;
objUser = null;
objSAPUser = null;
objService = null;
ep5User = null;
objUriReference = null;
strUrl = null;
strFileName = null;
strLevel = null;
}//end of finally
//@@end
}
You need to create a folder named AUT in the documents folder of your KM i.e go to Content Management->KM Content and create a folder there. Upload a file into this folder and your code should work fine .Just don't forget a WebDynpro Sharing Reference to <b>sap.com/com.sap.km.cm</b>
Hi Tahzeeb,
I have successfully implement the code. but o/p didn't come. i have following doubts regarding this
1. which UI element is used FileDownload or LinkToUrl?
2. context & attribute structure and datatype?
3. which attribute are set into UI element?
4. why u use int intCounter parameter in DownloadFile method?
5.where will called in DownloadFile method?
can u please tell me regarding this..
i need urgent...this
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
User | Count |
---|---|
92 | |
11 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.