cancel
Showing results for 
Search instead for 
Did you mean: 

How to create a Context attribute which can contain a file?

Former Member
0 Kudos

Hello,

I would like to create a context attribute which can contain a photo which is in a .jpeg file.

Creating an attribute with Java native type as File should do the job?

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

Hi,

You can pass the location of the file dynamically to the context and bind the image source to that context !!

Hope this should work !!

Regards, Anilkumar

Former Member
0 Kudos

it does 10x.

Answers (5)

Answers (5)

Former Member
0 Kudos

Hi,

You can get the server info as follows

HttpServletRequest request =Task.getCurrentTask()

.getWebContextAdapter()

.getHttpServletRequest();

wdContext.currentContextElement().setURL("http://"

+ request.getServerName()+ ":"+ request.getServerPort() "//" <<ImageName>>);

and place the file under the location "C:\usr\sap\J2E\JC00\j2ee\cluster\server0\apps\sap.com\com.sap.engine.docs.examples\servlet_jsp\_default\root"

Now if the file is present on the above location it will display the image on the view if you set this value to the context attribute.

Regards, ANilkumar

Former Member
0 Kudos

Please don't recommend using non-API classes.

Armin

Former Member
0 Kudos

First of all place the image file in the src -> mimes -> components -> name of the package.

Then create a context attribute of type string.

Set the context attribute to the image file name in the program.

Do get back for further clarifications.

Hope this helps you.

Regards,

Vijith

Former Member
0 Kudos

What exactly do you want to do?

You can use any Java type for context attributes, but if you want to bind UI element properties, it must be a Java DDIC type.

Armin

Former Member
0 Kudos

Can you please explain me what is Java DDIC type and which types can I bind to which UI Element?

Former Member
0 Kudos

Hi Roy,

For all the UI element properties u have corresponding Simple types available in local dictionary.

say For Visibility property of the UI elemnts U have

com.sap.ide.webdynpro.uielementdefinitions.Visibility.

When ever u want to bind attributes to the UI elements u can use the predefined simple types available in the local dictionary

Former Member
0 Kudos

U shuld create a Context Attribute of Type Binary

Thanks

Krishna kanth

Former Member
0 Kudos

Hi Roy,

u can create an attribute of type String which will hold the image name and place the jpeg file in the

\src\mimes\Components\..

In your UI elemnt u can just bind the attribute.

Actually in which scenario do u want to use this.Do u want to just bind it to an UI elemnt property or for any other purpose.

Regards,

Sowjanya.

Message was edited by: Sowjanya Chintala

Former Member
0 Kudos

I've created a string attribute which contains the path: C:
test.jpeg and I don't understand the result:

I set the test.jpeg on C drive on the server and it doesn't find the file, when I put it in my local C Drive it finds it. Shouldn't it take it from the server and not the client???

Former Member
0 Kudos

Place the image file in the in the folder

../src/mimes/components/ of ur project.

Former Member
0 Kudos

Yes but what if the images are sitting on the server and I want to take it from there on runtime?

Former Member
0 Kudos

I think this is a misunderstanding: Import your images in NWDS into folder src/mimes/<component_package> as suggested.

If you redeploy, the images will be copied to the server and the Web Dynpro runtime automatically creates the correct URL for you. In your UI Element property, e.g. Image.source, you can simply provide the image filename, like "photo.jpg".

Armin

Message was edited by: Armin Reichert

Former Member
0 Kudos

Yes but what if the file is being updated on the server every day?

BeGanz
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Roy,

for displaying dynamically loaded mime objects (e.g. retrieved via Adaptive RFC) you can follow this approach:

1.) Declare a context attribute 'File' of type binary (Should be done by context to model binding). Also declare another attribute 'FileName' of type string for storing the filename.

Define another context attribute 'URL' of type string for storing the dynamic image URL. The image UI-element is bound to this context attribute.

2.) Implement the following code in the wdDoInit()-method of the correct controller:

  public void wdDoInit()
  {
    /*@@begin wdDoInit()*/
    wdContext.currentContextElement().setImgVisibility(WDVisibility.NONE);

    /* Modify datatype, prepare datatype for modifications done by the runtime */
   wdContext.getNodeInfo().getAttribute("File").getModifiableSimpleType();
    /* @@end*/
  }

3.) Execute model class for retrieving the mime object from the backend. The binary mime data is now stored in the controller context. The filename is stored in attribute FileName.

4.) Generate URL for mime object via service class <b>WDWebResource</b>

...
    IWDCachedWebResource cachedResource = null;
    String imgUrl = null;

    WDWebResourceType type =
      ((IWDModifiableBinaryType) wdContext.getNodeInfo().getAttribute("File").getModifiableSimpleType())
        .getMimeType();
    byte[] file = wdContext.currentContextElement().getFile();

    if (file != null) {
      cachedResource = WDWebResource.getWebResource(file, type);

      try {
        imgUrl = cachedResource.getURL();
        wdContext.currentContextElement().setUrl(imgUrl);
        wdContext.currentContextElement().setImgVisibility(WDVisibility.VISIBLE);
      } catch (WDURLException e) {
        wdComponentAPI.getMessageManager().reportException(e.getLocalizedMessage(), true);
      }
    }
...

Result: The dynamically retrieved image is displayed in the image UI-element

Regards, Bertram