cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert string to binary for download-UI-element?

Former Member
0 Kudos

Hello!

I have a an EJB which delivers a String, which I want to be saved by the user in my WD project.

So I need to convert the incoming String to binary type in my context.

According to the tutorial "Uploading and Downloading Files" I created an event handler for a button with the following code:


IWDAttributeInfo attributeInfo = wdContext.getNodeInfo().getAttribute(IPrivateXmlManagerView.IExportFileElement.FILE_RESOURCE);
IWDModifiableBinaryType binaryType = (IWDModifiableBinaryType) attributeInfo.getModifiableSimpleType();

String outStream = XmlHandlerFacade.getInstance().exportXml();
wdContext.currentExportFileElement().setFileResource(outStream.getBytes());

binaryType.setFileName("myOutput.xml");
binaryType.setMimeType(WDWebResourceType.XML);		

This implementation just causes a NullPointerException.

When I only try to implement the conversion of the String to a Byte-Array like this...


String outStream = XmlHandlerFacade.getInstance().exportXml();
wdContext.currentExportFileElement().setFileResource(outStream.getBytes());

...there is an IllegalArgumentException:

java.lang.IllegalArgumentException

at com.sap.dictionary.runtime.DdTypeBinary.format(DdTypeBinary.java:61)

at com.sap.tc.webdynpro.clientserver.data.DataContainer.doFormat(DataContainer.java:1402)

at com.sap.tc.webdynpro.clientserver.data.DataContainer.getAndFormat(DataContainer.java:1092)

at com.sap.tc.webdynpro.clientserver.data.DataContainer.getAndFormat(DataContainer.java:1064)

at com.sap.tc.webdynpro.clientserver.uielib.standard.impl.FileDownload._getData(FileDownload.java:81)

... 84 more

Anybody knows how to do it right?! I imagine that it can't be so difficult...

Thanks,

Ramó

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Try this way,


String test = XmlHandlerFacade.getInstance().exportXml();
IWDResource resource = WDResourceFactory.createResource( test.getBytes(), "test", WDWebResourceType.XML);
wdContext.currentExportFileElement().setFileResource( resource);

Regards

Vinod V

Former Member
0 Kudos

Thanks for your answers. I tried to implement like you said, but the NWDS says, that

wdContext.currentExportFileElement().setFileResource(resource);

is not applicable for IWDResource arguments - instead of this only for byte[].

Perhaps this way is not available in my version? I'm working with NWDS 7.0 SP08 (actually it is not possible to upgrade to another version).

The code line

byte[] test = outString.getBytes();

doesn't cause an exception.

It happens only, when I want to do

wdContext.currentExportFileElement().setFileResource(test);

Edited by: Ramón Diegel on Aug 13, 2008 1:54 PM

Former Member
0 Kudos

Hi

I have tried this code and found working

The Context Value Attribute is of type Resource



// init method
String test = "testing for";
    IWDResource resource = WDResourceFactory.createResource( test.getBytes(), "tst", WDWebResourceType.XML);
    wdContext.currentContextElement().setResource( resource);

// Download action
IWDWindow window = wdComponentAPI.getWindowManager()
    	.createExternalWindow( wdContext.currentContextElement()
    		.getResource().getUrl( 0), "Test", false);
    window.open();

Result

Opens a new Window containing the string testing for in XML form

I am using NWDS 7.0 SP 09.

Regards

Vinod V

Former Member
0 Kudos

great job, thanks a lot!

it seems, that my problem was the ExportFileElement, which I used instead of a usual CurrentContextElement.

Now I can go on this way...

Greetings,

Ramó

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi,

First check if "outStream" is null

Former Member
0 Kudos

Hi! yes, the "real" implementations is surrounded by an IF:


if (outStream == null) {
	wdThis.wdFirePlugToErrorView("XML could not be generated!\nUnknown error.", "XML Export");
} else {
	wdContext.currentExportFileElement().setFileResource(outStream.getBytes());
}

The simple output of the string itself works.