cancel
Showing results for 
Search instead for 
Did you mean: 

File resource null

Former Member
0 Kudos

Hi experts,

        I am developing the webdynpro java Component where i am uploading and downloading the files to the server and i am storing the files in ECC.I am able to upload the file successfully into the server and i am storing the files in ECC,but when i tried to download the fille which is stored in ECC i am getting the file resource as null.

I have used the following code to create resource to download the file

try

     {

   //   wdComponentAPI.getMessageManager().reportSuccess("test");

         FILE_NAME=wdContext.currentContextElement().getFileResource().getResourceName();

         String resourcePath =

         WDURLGenerator.getResourcePath(

         wdComponentAPI.getDeployableObjectPart(), PopupView.FILE_NAME);

       

         IWDResource resource =

         WDResourceFactory.createResource(

         new FileInputStream(new File(resourcePath)),

       PopupView.FILE_NAME,

       PopupView.FILE_EXT,

        true);

        wdContext.currentContextElement().setFileResource(resource);

       //  wdComponentAPI.getMessageManager().reportSuccess("file inside the block"+wdContext.currentContextElement().getFileResource());

         } catch (WDAliasResolvingException e) {

          wdComponentAPI.getMessageManager().reportException(

          e.getLocalizedMessage(), true);

          } catch (FileNotFoundException e) {

         

          }

In the above code ,i am getting "resource" as null.

    

Regards,

Anil Kumar

Accepted Solutions (1)

Accepted Solutions (1)

ErvinSzolke
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi,

is the variable resourcePath correctly pointing to the path AND the filename ?

You can test it quickly with another reportSuccess(resourcePath) message or via debugging.

Try also with method createCachedResource().

For me this code works for getting PNG images from the server I uploaded there previously:

public String getResourceURL(String filename) {
  IWDResource resource = null;

  try {
   resource = WDResourceFactory.createCachedResource(new FileInputStream(new File(filename)),
                 "",
                 WDWebResourceType. PNG,
                 true);

   String url = resource.getUrl(0);
   return url;

  } catch (FileNotFoundException e) {
   e.printStackTrace();
  }
  return null;
}

Then I set the returned String to the source parameter of the image.

I hope this helps.

Cheers,

Ervin

Former Member
0 Kudos

Hi Ervin,

  In my previous code i am getting resourcePath correctly,but when i tried to create resource using blow method i am getting the null value.

  IWDResource resource =

         WDResourceFactory.createResource(

         new FileInputStream(new File(resourcePath)),

       PopupView.FILE_NAME,

       PopupView.FILE_EXT,

        true);

I have used createCachedResource() method as you told,now i am able to get the resource now but when i try to download the text or excel file i am getting the  error like

The image "resource" cannot be displayed,because it contains errors.

i have used the below code

IWDResource resource = null; 

           try { 

            resource = WDResourceFactory.createCachedResource(new FileInputStream(new File(wdContext.nodeEx_Table().getEx_TableElementAt(i).getFilename())),  

                          "",  

                          WDWebResourceType.PNG, 

                          true);  

            wdContext.currentContextElement().setFileResource(resource);

            String url = resource.getUrl(0);

            wdComponentAPI.getMessageManager().reportSuccess(""+url);

            name.setFileResou(wdContext.currentContextElement().getFileResource());

      //            name.setFileResou(value)

           } catch (FileNotFoundException e) { 

            e.printStackTrace();

           }

My requirement is not only to download PNG files ,i have to download .txt,.xls,.png all kinds of files.

ErvinSzolke
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi,

for me this code works like a charm:

- I only added a button to the UI that has an onActionDownload() event.

- let's assume that on the server you have two files test.txt and img.png

- the below code works for both on my end

public void onActionDownload(com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent wdEvent )
//@@begin onActionDownload(ServerEvent)

   String path = "C:\\";                 // do not forget to escape the backslash

   String filename = "test.txt";    //if you replace it to img.png the app will still work

   IWDResource resource = null;

   try {
    resource = WDResourceFactory.createCachedResource(new FileInputStream(new File(path+filename)),
                                                                                                                                  filename,
                                                                                                                                  WDWebResourceType.UNKNOWN,
                                                                                                                                  true);

   } catch (FileNotFoundException e) {
    e.printStackTrace();
   }

   resource.download(WDFileDownloadBehaviour.ALLOW_SAVE.ordinal());
//@@end
  }

I hope this helps.

Regards,

Ervin

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi Ervin,

Strill i am facing the issue ,i am getting the file resource as null.

I am setting file name and file path correctly.

At the time of uploading the document i am setting the path manually like

file path =Ext_Path+"\\"+file name+"_"+new Timestamp(date.getTime())+"."+extension

   In above Ext_Path = "D:\\DOCUMENTS" where i am getting the Ext_Path from different RFC .

OnInit() i am calling the filepath and file name from  RFC.As you suggested i have used the below method still i am facing the issue.

resource = WDResourceFactory.createCachedResource(new FileInputStream(new File(path+filename)),                                                                                                                                    filename,                                                                                                                                    WDWebResourceType.UNKNOWN,                                                                                                                             true); 

resource.download(WDFileDownloadBehaviour.ALLOW_SAVE.ordinal()); // here i am unable to download because i am getting the resorce as null

When i replace the new File(path+filename) with new File(filename) i am getting the first file resource and i dont know what is happening.

Please suggest me how to resolve the issue.

Regards,

Anil