cancel
Showing results for 
Search instead for 
Did you mean: 

FileDownload - Download string to file

Former Member
0 Kudos

Hi,

I want to download some content to a file.

I have created a FileDownload element in my layout and i have bound the resource property.

In my context node i have made a value attribute of type <b>com.sap.tc.webdynpro.progmodel.api.IWDInputStream</b>

For this value attribute i have set the calculated property to true and also read only. In the generated getter i have the following code:


String test = "" + System.currentTimeMillis();
IWDInputStream is = WDResourceFactory.createInputStream(test.getBytes("UTF-8"));
return is;

in wdDoInit i have bound the WDResource to the attributepointer of my inputstream value attribute and i did: wdContext.currentContextElement.setResource(resource);

The code works fine, the only problem is that the content of the file is ALWAYS the value generated the first time the getter is called....(Its also possible he only calls the method only the first time. i'm not sure, but he should call the getter automatically when i click my FileDownload element because the Resource needs the inputstream then)

Is there some cache i need to clear every time or so? Or am i missing something?

The code below does work, but i want to have working with the FileDownload Element too:


String bla = "" + System.currentTimeMillis();
IWDResource resource = WDResourceFactory.createResource(bla.getBytes(), "test.xml", WDWebResourceType.TXT);
IWDWindow window = wdComponentAPI.getWindowManager().createNonModalExternalWindow(
			resource.getUrl(WDFileDownloadBehaviour.ALLOW_SAVE.ordinal()), "Window title");
window.show();
window.destroyInstance();

With any of above methods, is it possible to close the window that is generated when i save?

Greetz,

J.

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Check <a href="http://help.sap.com/saphelp_nw04s/helpdata/en/42/f6ec5a09321bc7e10000000a11466f/content.htm">this</a>.

Regards,

Christophe

Former Member
0 Kudos

Hi,

I checked, i did the same as in the example and i can save the file, only the data is always the same.

Apparantly the getter method of my calculated attribute is only called the first time... How can i avoid this?

Kind regards.

J.

Message was edited by:

Joren Crauwels

BeGanz
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hallo Joren,

an already streamed resource will not be re-calculated again after the first invocation of the calclated context attribute getter method. After the first invocation of the getter method, the resource is streamed to the client and keeps cached in the Web Dynpro Binary Cache. The Web Dynpro Runtime does not re-invoke the calculated context attribute getter method for a completely initialized, cached resource which is allready streamed to the client. Only in case the context attribute stores a 0-byte resource comprising no content the calculated context attribute getter method gets invoked.

I actually do not know a workaround to fulfill your requirement: to stream a new resource to the client on-demand repeatedly.

Regards, Bertram

Former Member
0 Kudos

Hello,

Thx for you reply. I allready found a workarround for it.

I created an Event(newData) and an eventhandler onNewData:

In the eventhandler i do the following:


IWDAttributePointer attributePointer = wdContext.currentContextElement().getAttributePointer("XMLData");
IWDResource reportResource = WDResourceFactory.createResource(
				attributePointer,
				"r3tabledata.xml", 
				WDWebResourceType.XML
				);
wdContext.currentContextElement().setXMLResource(reportResource);

Now the file contains the right data every time i save.

Kind regards,

J.

Former Member
0 Kudos

Hi,

I just found the solution for problem of downloading the same content. The thing is if the data has change but the resource of the file doesn't. One more problem is that if you want to use FileInputStream for downloading file, you could not close it. However, method createResource of WDResourceFactory has three constructor, you need to use the one (FileInputStream,<<filename>>,<<resourceType>>, true); (true option is to flush the FileInputStream and close it).

Finally, you need to put the part of code for supplying the resource at the part of method wdDoModifyView(). The reason to put it here is for every even need to load the view again, and when loading it, the resource will also be reloaded too. If not, the resource of the file will be not load and be cached in the memory.

here is the part of code.

public static void wdDoModifyView( IPrivateTestExport_Main wdThis, IPrivateTestExport_Main.IContextNode wdContext, com.sap.tc.webdynpro.progmodel.api.IWDView view, boolean firstTime){

try {

// ===== WORKAROUND CODE ===========================

// The image file 'sap.jpg' is deployed with the Web Dynpro project

// (under src/mimes/Components...). The resource path (URL) for this mime

// objects can be accessed using the WDURLGenerator service.

String resourcePath = "testfile.txt";

// retrieve resource object for given resource path and create

// a new object of type IWDResource by invoking the

// WDResourceFactory API.

IWDResource resource =

WDResourceFactory.createResource(

new FileInputStream(new File(resourcePath)),

"testfile.txt",

WDWebResourceType.TXT,

true);

// true: flush file input stream so that it gets closed immediately

// ===== SIMPLE CODE only running in NW 04s Stack 12 ========

// Based on a bug this simple coding cannot be implemented. The bug

// will be fixed within NW04s SP Stack 12.

/*IWDResource resource =

WDWebResource.getWebResource(

wdComponentAPI.getDeployableObjectPart(),

FileDownloadView.FILE_EXT,

FileDownloadView.FILE_NAME);*/

// store resource object in context attribute 'FileResource'

wdContext.currentResourceElement().setDownloadFileResource(resource);

} catch(FileNotFoundException ex){

}

}