cancel
Showing results for 
Search instead for 
Did you mean: 

file download

Former Member
0 Kudos

I have one table, preceded by a radio button for each row. After checking the radio button when the user press "DOWNLOAD" button, the corresponding file should download.

I may get the url after some days. But could any one give me the generalized code for this.

I repeat.... selecting a radio button ad clicking a action button should cause the file download mechanism.

Accepted Solutions (1)

Accepted Solutions (1)

Abhinav_Sharma
Contributor
0 Kudos

Hi Pradeep,

You want to download a file when some condition is satisfied (in ur case radio button and download button). Following is the code given to download a file (taken form standard help provided by NWDS)

// The FileDownload UI element requires:

IWDAttributeInfo attInfo = wdContext.nodeRoot().getNodeInfo().getAttribute("FileDownLoad");

binaryType = (IWDModifiableBinaryType) attInfo.getModifiableSimpleType();

// in addition the FileDownload UI element needs for defined resource types

binaryType.setFileName("Snow Trees.jpg");

binaryType.setMimeType(WDWebResourceType.JPG_IMAGE);

//The FileDownload UI element needs for undefined resource types

binaryType.setFileName("Snow Trees.xcc");

binaryType.setMimeType(new WDWebResourceType("xcc", "", true));

This way you can download any file.

XXXXXXXXXXXXXXXXXXXXXXXXXXX.

Abhinav

Message was edited by:

Armin Reichert

Answers (1)

Answers (1)

sid_sunny
Contributor
0 Kudos

Hi Pradeep,

You can easily use File Download UI element.

Simply bind the onSelect property of the radio button to an action and in that action write the code to intialize the context which is storing the binary data of the file with the appropriate file. The sample code is given below

<b>IWDAttributeInfo attInfo =

wdContext.currentContextElement().node().getNodeInfo().

getAttribute("PdfSource");

ISimpleTypeModifiable type = attInfo.getModifiableSimpleType();

IWDModifiableBinaryType binaryType = (IWDModifiableBinaryType) type;

binaryType.setFileName("TravelRequest.pdf"); // Set file name

binaryType.setMimeType(WDWebResourceType.PDF); // set the mime type

String fileName = <Your File path>

/

/ path to the source file

/* converts the binary file to a byte array and moves it to the text element */

contry

{

File file = new File(fileName);

FileInputStream in = new FileInputStream(file);

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();

wdContext.currentContextElement().

setPdfSource(out.toByteArray());

} catch(Exception e)

{

throw new WDRuntimeException(e);

}</b>

Regards

Sid

Former Member
0 Kudos

Where i need to write this code.? in "onSelect" of Radio button or for Download Button?

sid_sunny
Contributor
0 Kudos

Hi Pradeep,

First create an action bind it to the onSelect property of the radio button and in this action write the code posted by me. So that when you click on the file download link the correct file will get downloaded.

Regards

sid

Former Member
0 Kudos

Hi sid,

Right now i dont have J2ee server to deploy and check wheter your code is working or not for me. But one basic doubt, am I need to use "file download" UI element or not? If yes, the file download UI will be a like a link( on which we need to click to download). But i need this action will perform in a button not as a link.

could you help me in regards.

Abhinav_Sharma
Contributor
0 Kudos

HI

onAction of your download button.

Abhinav Sharma

sid_sunny
Contributor
0 Kudos

Hi Pradeep,

If you dont want to use the File Download UI element then you can write the following code in the onAction associated to your button:

first read the file as mentioned in my code and then

<b>String FileName = file.getAbsoluteFile().getName();

IWDCachedWebResource webResource = WDWebResource.getWebResource(fileData, WDWebResourceType.PDF);

window = wdComponentAPI.getWindowManager().createExternalWindow(webResource.getURL(), "Report", false);

window.open();</b>

You can refer to this link too.

Do reward points if it helps.

Regards

Sid