cancel
Showing results for 
Search instead for 
Did you mean: 

Image Upload and Download

Former Member
0 Kudos

Hi All

I am working on Netweaver2004s

The scenario is like

I have a webdynpro application where in I need to Upload , Display the image in browser and then FTP the image to a server .

I have created a FileUpload type UI and bound it to binary type attribute say Datasource.

Now

<b>byte[] imgData = wdContext.currentContextElement().getDatasource();</b>

gives byte[] and this imgData I have been using in my FTP code.

1) Now please let me know since IWDModifiableBinaryType APIs has been replaced by <b>IWDResource</b> and <b>WDResourceFactory</b>, How can i proceed with getting the byte Array ?

2) I have an Image UI, I have been using <b>IWDCachedWebResource</b>, to convert the URLString to Image.

<b>IWDCachedWebResource res =

WDWebResource.getWebResource(

imgData,

WDWebResourceType.JPG_IMAGE);</b>

Now using <b>res.getAbsoluteURL(),</b> i am getting the string which i have bound to Image UI.

Now IWDCachedWebResource has also deprecated.

Can u pls help in my scenario, how can i replace IWDModifiableBinaryType and IWDCachedWebResource with IWDResource APIs.

Regards

Sonal Mangla

Accepted Solutions (0)

Answers (5)

Answers (5)

Former Member
0 Kudos

Hi ,

For upload use this code:

InputStream text = null;

int temp = 0;

try

{

File file = new File(wdContext.currentContextElement().getFileResources().getResourceName());

FileOutputStream op = new FileOutputStream(file);

if(wdContext.currentContextElement().getFileResources()!=null)

{

text = wdContext.currentContextElement().getFileResources().read(false);

while((temp=text.read())!=-1)

{

op.write(temp);

}

}

op.flush();

op.close();

String path = file.getAbsolutePath();

wdComponentAPI.getMessageManager().reportSuccess("File has uploaded Sucessfully:\n"+path);

}

catch(Exception e)

{

e.printStackTrace();

}

For Download:

/*********************************For Downloading PDF**********************************************/

it should be in wdDoInit().

String fname = wdContext.currentEx_Project_Search_ResultsElement().getProjectid().toString();

String FILE_NAME =fname+ ".pdf";

WDWebResourceType FILE_EXT = WDWebResourceType.PDF;

try {

String resourcePath = "E:/usr/sap/CR5/DVEBMGS01/j2ee/cluster/server0/" + FILE_NAME;

//wdComponentAPI.getMessageManager().reportSuccess(resourcePath);

IWDResource resource = WDResourceFactory.createResource( new FileInputStream(new File(resourcePath)),FILE_NAME,FILE_EXT,true);

wdContext.currentContextElement().setFileResource(resource);

}

catch(Exception e)

{

e.printStackTrace();

}

<b></b><i></i><u></u>Place this code in end of the view implementation, it should be between begin and end statements:

private byte[] getByteArrayFromResourcePath(String resourcePath)

throws FileNotFoundException, IOException {

FileInputStream in = new FileInputStream(new File(resourcePath));

ByteArrayOutputStream out = new ByteArrayOutputStream();

int length;

byte[] part = new byte[10 * 1024];

while ((length = in.read(part)) != -1) {

out.write(part, 0, length);

}

in.close();

return out.toByteArray();

}

<b></b><b></b><b></b><i></i><b></b><b></b><i></i><u></u>

former_member751941
Active Contributor
0 Kudos

Hi Sonal,

Here is the code for byte array.

String resourcePath =

WDURLGenerator.getResourcePath(

wdComponentAPI.getDeployableObjectPart(),

FileDownloadView.FILE_NAME);

// retrieve byte array for given resource path and store it

// in context value attribute 'FileResource'

wdContext.currentContextElement().setFileResource(

this.getByteArrayFromResourcePath(resourcePath));

//--


return s byte array--


/**

  • Returns a byte array for a given file resource path

*/

private byte[] getByteArrayFromResourcePath(String resourcePath)

throws FileNotFoundException, IOException {

FileInputStream in = new FileInputStream(new File(resourcePath));

ByteArrayOutputStream out = new ByteArrayOutputStream();

int length;

byte[] part = new byte[10 * 1024];

while ((length = in.read(part)) != -1) {

out.write(part, 0, length);

}

in.close();

return out.toByteArray();

}

//----


// store image file name in constant FILE_NAME

private static final String FILE_NAME = "sap.jpg";

<a href="https://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/202a850a-58e0-2910-eeb3-bfc3e081257f">Uploading and Downloading Files in Web Dynpro Java</a>

<a href="https://www.sdn.sap.comhttp://www.sdn.sap.comhttp://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/00062266-3aa9-2910-d485-f1088c3a4d71">Uploading and Downloading Images in Web Dynpro Java</a>

Regards,

Mithu

Former Member
0 Kudos

Hi Mangla,

For downloading use this

Create a context attribute and set calculated as true then u will get a getter method,In that u write code like this

IWDInputStream stream = null;

try {

stream = WDResourceFactory.createInputStream(new FileInputStream(new File(<give the location of a file u want to download>));

}

catch (Exception e)

{

e.printStackTrace();

}

return stream;

for open file u set "resource"as Resource type context attribute

Former Member
0 Kudos

Hi,

The files are storing in a server not in "src/mimes/Components/<package>".

Could you reply on how to download a file stored in server.

I think WDResourceFactory, IWDInputStream are working for the files stored in

"src/mimes/Components/<package>".

Thanks,

sreeni

Former Member
0 Kudos

Hi Sathish

Thanks for the effort. I gor the Stream, But how can i convert it to byte array.

Either we have to create InputStream

<b>InputStream imgDataInp =

wdContext.currentContextElement().getDatasource().read(true);

byte[] imgData = new byte[imgDataInp.available()];</b>

But its not working

and what is to be done 2nd point mentioned

Regards

Sonal Mangla

oliver
Product and Topic Expert
Product and Topic Expert
0 Kudos

Hi Sonal,

as you are working on NW70, you should check out Bertram Ganz' Weblogs about FileUpload and FileDownload in WebDynpro where he explains hot to use the WDResource classes in detail.

/people/bertram.ganz/blog/2007/05/25/new-web-dynpro-java-tutorials--uploading-and-downloading-files-in-sap-netweaver-70

Here is the corresponding tutorial

https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/00062266-3aa9-2910-d485-f1088c3a...

and sample project:

https://www.sdn.sap.com/irj/sdn/go/portal/prtroot/docs/library/uuid/30398c47-40a9-2910-4aae-cec9b96d...

Regarding the image: I usually place them in the project under "src/mimes/Components/<package>".

Next create a value attribute and put the filename of the image in there. That one you have to use as a "source" attribute in your ImageUI. That should work.

Best regards,

ok

Message was edited by:

Oliver Kohl

Former Member
0 Kudos

Hi Mangla,

For uploading use this code

try{

String Upload=wdContext.currentContextElement().getResource().getResourceName();

FileOutputStream fout=new FileOutputStream(new File(upload));

}

catch(Exception e)

{

e.getStackTrace();

}