Uploading and Downloading Files in Web Dynpro Java - 0 - 1
Uploading and Downloading Files in Web Dynpro Java - 0 - 1
Uploading and Downloading Files in Web Dynpro Java - 0 - 1
Summary
In this tutorial you learn how to download and upload files within Web Dynpro applications running on SAP
NetWeaver 04s by utilizing the new dictionary type Resource and its related Web Dynpro APIs
IWDResource and WDResourceFactory. In contrast to the dynamic type modification of a binary context
attribute in SAP NetWeaver 04, the new dictionary simple type Resource yields a fully declarative, zero
coding data transport of download and upload resources between Web Dynpro client and controller context
on server side.
Note
This tutorial covers the basic principles of Uploading and Downloading Files in Web Dynpro Java in SAP
NetWeaver 2004s. You can read more advanced technical details in the related SDN article Uploading and
Downloading Files in Web Dynpro Tables. Both articles are based on the same Web Dynpro sample
application.
Author Bio
After his studies in mathematics, physics and computer science Bertram Ganz finished his
teacher training at a German grammar school stressing technical sciences. He has been a
member of the Web Dynpro Java Runtime development team (SAP NetWeaver ESI
Foundation UI) since 2002. The main focus of his work is on knowledge transfer, rollout, and
documentation. Bertram regularly publishes articles on Web Dynpro in the context of the SAP
NetWeaver Application Server. Bertram is co-author of the books Maximizing Web Dynpro
for Java and Java Programming with the Web Application Server.
Table of Contents
Uploading and Downloading Files in Web Dynpro Java ................................................................. 1
Applies to: ........................................................................................................................................ 1
Summary.......................................................................................................................................... 1
Note ................................................................................................................................................. 1
Author Bio ........................................................................................................................................ 1
Table of Contents ............................................................................................................................ 2
Introduction ...................................................................................................................................... 3
Whats New in SAP NetWeaver 04s ............................................................................................ 3
Using the Virus Scan Interface ........................................................................................................ 4
Preview ............................................................................................................................................ 4
Objectives .................................................................................................................................... 5
Prerequisites ................................................................................................................................ 5
Importing the Project Template ....................................................................................................... 6
Prerequisites ................................................................................................................................ 6
Importing the Project Template into the SAP NetWeaver Developer Studio............................... 6
Tabular Project Structure ............................................................................................................. 7
Using the UI Elements FileUpload and FileDownload..................................................................... 8
Uploading Files ................................................................................................................................ 8
Defining the Context and View Layout......................................................................................... 8
Implementing the FileUploadView Controller............................................................................. 10
Running the Application - File Upload........................................................................................ 12
Downloading Files ......................................................................................................................... 13
File Download Defining the Context and View Layout............................................................ 13
Implementing the FileDownloadView Controller ........................................................................ 14
Running the Application - File Download ................................................................................... 17
Result ......................................................................................................................................... 17
Related Content............................................................................................................................. 18
Copyright........................................................................................................................................ 19
Introduction
The Web Dynpro UI Element Library provides two special UI elements (FileDownload and FileUpload) with
which you can download files from the Web Dynpro runtime environment or upload them there. This is
performed using declarative data binding. Here, Web Dynpro runtime automatically transports different types
of MIME files between the client-side user interface or UI element and the server-side controller context.
In contrast to the semi-declarative approach in SAP NetWeaver 04, which was based on invoking the
IWDModifiableBinaryType-API in the controller code, the new dictionary simple type Resource within
SAP NetWeaver 04s yields a fully-declarative data transport of MIME resources between Web Dynpro client
and controller context on server side.
This tutorial will show you how to upload and download MIME files using the UI elements FileUpload and
FileDownload.
Whats New in SAP NetWeaver 04s
In SAP NetWeaver 04s file download and upload was significantly enhanced and simplified with the following
new features:
New Dictionary Type Resource: The new Java dictionary type Resource allows to bind FileUpload
and FileDownload UI elements to context attribtues storing MIME files named resources. In SAP
NetWeaver 04 the primitive type binary must be used.
New Java Type IWDResource: The new Web Dynpro interface IWDResource is the Java type
counterpart of the dictionary type Resource. It allows to store the file content (binary resource data)
and the file metadata (MIME type, resource name) in one object. Consequently the resource
metadata (MIME type, file name) must no longer be stored in the context attribute info
(IWDAttributeInfo) using a modifiable binary type and may therefore differ among multiple
resources or node elements stored in the same context node.
New Web Dynpro Factory WDResourceFactory: Resource objects of type IWDResource can
easily be created with the new Web Dynpro factory class WDResourceFactory. This factory class
significantly simplifies the implementation of file download scenarios where statically deployed or
dynamically created resources must be stored in the context.
Zero Coding File Upload: With the new Java dictionary type Resource it is no longer needed to
implement the type modification of a binary context attribute by invoking the
IWDModifiableBinaryType API in the controller code. Consequently file upload can be realized
in a purely declarative, zero coding approach. You just have to bind a FileUpload UI element to a
context attribute of type Resource. The Web Dynpro Runtime then automatically transports the
uploaded file to the context attribute as an object of type IWDResource.
New File Download Behaviors: The behavior of the FileDownload UI Element can now be defined
with the new Java dictionary type FileDownloadBehavior. Its enumeration specifys three different file
download behaviors: open resource in-place without opening a dialog window, save resource in
local file system (open dialog) and open resource depending on the MIME type of the downloaded
file (open dialog).
Downloading Files in Tables On-Demand: The new on-demand streaming technique allows to
download the resource content on-demand when the user actually requests it on client side.
Especially when using the FileDownload UI element as table cell editor this new technique yields a
heavily reduced context memory consumption on server side.
Read the related SDN article Uploading and Downloading Files in Web Dynpro Tables to get more
information on the additional technical details.
Preview
The screenshots displayed below show the three views in the tutorial application.
When the application is launched, the WelcomeView appears, where you can navigate to the scenarios file
upload and file download:
In the FileDownloadView, you can download an image file, which is deployed in the example project, from
the controller context and display it on the user interface.
The file download behavior can be defined by clicking one of three radio button. Figure 3 shows that the file
download behavior ALLOWSAVE opens a dialog window to save the downloaded resource on the local file
system. With the behavior OPENINPLACE the resource is instantly displayed in another browser window.
Assign the dictionary simple type Resource to context attributes so that they can store MIME files.
Create objects of type IWDResource from an image resource deployed with the project by invoking
the WDResourceFactory API.
Prerequisites
Systems, Installations, and Authorizations
Knowledge
The project template TutWD_FileUpDownload_NW04s_Init (the starting point for this tutorial)
The projects can be downloaded as zip files under SDN Home Developer Areas Web Application
Server Web Dynpro How-To Guides Web Dynpro Sample Applications and Tutorials .
Prerequisites
Importing the Project Template into the SAP NetWeaver Developer Studio
1. Call the SAP Developer Network at http://sdn.sap.com and log on with your user ID and password. If
you do not have a user ID yet, you need to register before you can proceed.
2. Choose Home Developer Areas Web Application Server Web Dynpro How-To Guides
Web Dynpro Sample Applications and Tutorials and then choose the section UI Elements.
3. Navigate to the example application (38) - Web Dynpro FileUpload and FileDownload.
4. Download the zip file TutWD_FileUpDownload_NW04s_Init.zip containing project template
TutWD_FileUpDownload_NW04s_Init and save the file to any directory on your local hard disk or
directly in the SAP NetWeaver Developer Studio work area.
5. Extract the content of the zip file TutWD_FileUpDownload_NW04s_Init.zip in the work area of
the SAP NetWeaver Developer Studio or in any directory on your local hard disk.
6. Start the SAP NetWeaver Developer Studio.
7. Import the Web Dynpro project TutWD_FileUpDownload_NW04s_Init:
8. In the menu, choose File Import
9. In the next window, choose Existing Project into Workspace and click Next to confirm.
10. Choose Browse, open the folder in which you saved the project
TutWD_ FileUpDownload_NW04s _Init, and select this project.
11. Confirm by choosing Finish
12. The Web Dynpro Explorer now displays the Web Dynpro project
TutWD_FileUpDownload_NW04s _Init, allowing you to edit it and work through the tutorial.
File Upload
File Download
Both sections comprise information on how to define controller context and view layout, how to implement
the controllers and how to run the sample application.
Uploading Files
Defining the Context and View Layout
In this step, you define a properly typed context attribute in the FileUploadView context and then bind a
FileUpload UI element to it.
Declaring a Context Attribute of Type Resource
To save a MIME file in the controller context after it has been uploaded from the user interface to the server,
you first need to define a context attribute of type com.sap.ide.webdynpro.uielementdefinitons.Resource.
The Resource type is a special dictionary simple type for MIME resources (figure 4, point 1). At runtime the
controller context attribute stores the MIME resource in an object of type
com.sap.tc.webdynpro.services.sal.datatransport.api.IWDResource (figure 4, point 2).
The dictionary simple type Resource and the IWDResource-API was newly introduced in SAP
NetWeaver 2004s. The dictionary type Resource yields a fully-declarative data transport of
MIME resources between Web Dynpro client and controller context as the
IWDModifiableBinaryType-API must no longer be invoked in your custom controller
coding. Furthermore this new simple type makes it possible to store the metadata of a MIME
resource on attribute-level but not on attribute info level. Consequently a multiple context node
can store MIME resource of different types (Adobe PDF document, GIF image, Microsoft Word
Document etc.) in its node elements context attributes of type Resource.
UI / View Layout
UI-Element
FileUpload
Browse
Property
resource
Databinding
Java Dictionary
Simple Types
Attribute Type
ddic:com.sap.ide.webdynpro
.uielementdefinitons.Resource
com.sap.tc.webdynpro.services.sal.datatransport.api.
Design Time
IWDResource
Runtime
Value
Event onAction
UploadFile
Result
In this step, you have defined data binding between a FileUpload UI element and a context attribute of type
Resource (see figure 4). To start uploading a file on the user interface, you have also added a new Button UI
element and bound its onAction event to the UploadFile action.
You have bound the property resource of the FileUpload UI element to the above context attribute.
UI / View Layout
UI-Element
FileUpload
C:/SAP.jpg
type
WDWebResourceType
Roote Node
Roote Node
Element
Browse
Property
resource
name
String
FileResource
IWDResource
Databinding
Object
IWDResource:
resource
URL
String
Runtime Time
fileSize
String
stream
InputStream
2. Add the required import rows using the key combination STRG+SHIFT+O.
3. In the Web Dynpro tools, save the current project metadata by choosing
The size of the retrieved MIME resource is calculated within the private method getFileSize() which is
added to the last user coding area between the lines //@begin and //@end:
getFileSize() FileUploadView.java
//@@begin others
/** Read resource and calculate file size.
* @return the file size in Bytes, KB or MB as String */
private String getFileSize(IWDResource resource) {
InputStream stream = null;
DecimalFormat myFormatter = new DecimalFormat("###.##");
double size = 0;
String unit = "";
try {
stream = resource.read(false);
size = stream.available();
if (size < 1024) {
unit = " Bytes";
} else if (size < 1048576) {
size = size / 1024;
unit = " KB";
} else if (size < 1073741824) {
size = size / 1024 / 1024;
unit = " MB";
}
} catch (IOException e) {
wdComponentAPI.getMessageManager().reportException(
e.getLocalizedMessage(), true);
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
wdComponentAPI.getMessageManager().reportException(
e.getLocalizedMessage(), true);
}
}
}
return myFormatter.format(size) + unit;
}
//@@end
You have made sure that the SAP J2EE Engine has been launched.
Procedure
Save all Metadata
1. Save the current status of the metadata for your project.
Rebuild Project
2. In the Web Dynpro Explorer, open the context menu for the project node
TutWD_FileUpDownload_NW04s_Init and choose Rebuild Project.
Deploy new archive and run
3. Open the context menu for application object
and run.
Result
Test your application by clicking on the File Upload link. By choosing the Browse button, you can now select
a file to upload from your local file system. To start uploading, choose Upload File. Once the file has been
uploaded, the system displays details of the uploaded file and a success message.
Downloading Files
File Download Defining the Context and View Layout
In this step, you will define data binding between a FileDownload UI element and a Resource-type context
attribute.
Declaring a Context Attribute of Type Resource
When the file is downloaded, a MIME file saved in the context is downloaded by the client. To do this, first
define a new context attribute of dictionary simple type Resource in the view context.
1. Switch to the FileDownloadView (TutWD_FileUpDownload_NW04s_Init Web Dynpro Web
Dynpro Components FileUpDownloadComp Views FileDownloadView)
2. Choose the Context tab page and add a value attribute with the name FileResource.
in column Value of property type
3. Switch to the perspective view Properties and press the button
to define the attributes datatype.
4. In the type selection window select the Dictionary Simple Type
com.sap.ide.webdynpro.uielementdefinitons.Resource in the Java dictionary (see section 4, step 4 of
this tutorial).
Binding a FileDownload UI Element in the View Layout
After defining the Resource-type context attribute FileResource, you can bind a new FileDownload UI
element to it. This UI element is displayed as a link on the user interface. When this link is selected, the
MIME file saved in the context attribute is automatically transported to the user interface. With the transfer of
the associated MIME type, the connected software on the client side can be started in order to view the
MIME file.
5. Choose the Layout tab page.
6. In the perspective view Outline, add an UI element of type FileDownload with the name
FileDownload1 as a child of the FileDownload group. Move this element in front of the Button UI
element BackButton.
7. Define individual UI element properties according to the following table.
Properties
Value
MatrixHeadData
Download File!
Web Dynpro defines _blank as the default value for the property target in the UI elements
FileDownload and LinkToURL. Although the target values _top, _parent, and _self can be used,
you are advised not to do so, as this will cause the Web Dynpro application to crash.
Whereas target value _blank always causes a new target window to open, entering a target
value like "SameWindow" causes a downloaded file to always open in the same target window
(not the window of the application) if the UI element FileDownload is clicked more than once.
In the case of UI element LinkToURL, an exit plug with URL parameters should be used instead
of the target value _self.
Note that no events are bound to an action when using UI element FileDownload. Nevertheless
the downloadable resource can be retrieved from the server on-demand, this means after it is
requested by the user. This on-demand streaming technique is elaborately described in the
related SDN article Uploading and Downloading Files in Web Dynpro Tables.
Result
In this step, you have defined data binding between a FileIDownload UI element and a Resource-type
context attribute.
Implementing the FileDownloadView Controller
In this section the controller implementation required for file downloads is restricted to method wdDoInit(),
which is called when the view controller is created. Unlike file upload, no action event handling takes place
after the download process has been triggered by the user. The MIME file selected for downloading is
therefore stored in the context at the controller initialization stage.
When using the FileDownload UI element as a table cell editor you should apply the ondemand stream technique which is described in a separate SDN article Uploading and
Downloading Files in Web Dynpro Tables. With this approach you must not initially store all
resources in the context before the user actually requests them on client side.
Prerequisites
You have bound the property resource of the FileDownload UI element to the above context attribute.
com.sap.tc.webdynpro.services.sal.datatransport.api
com.sap.tc.webdynpro.services.sal.services.api
Classes
Interfaces
New in
SAP NetWeaver 04s
creates
IWDInputStream
IWDInputStream
IWDResource
IWDResource
creates
WDResourceWDResourceFactory
Factory
WDURLGenerator
WDURLGenerator
WDWebResource
WDWebResource
WDWebWDWebResourceType
ResourceType
extends
IWDCachedIWDCachedResource
Resource
IWDWebResource
IWDWebResource
creates
extends
IWDCachedIWDCachedWebResource
WebResource
WDFileDownloadWDFileDownloadBehavior
Behavior
creates
The name of the image file sap.jpg and its MIME type are stored in the two controller class constants
FILE_NAME and FILE_EXT:
//@@begin others
// store image file name and file extension in member constants
private static final String FILE_NAME = "sap.jpg";
private static final WDWebResourceType FILE_EXT =
WDWebResourceType.JPG_IMAGE;
//@@end
2. Add the required import rows using the key combination STRG+SHIFT+O.
3. In the Web Dynpro tools, save the current project metadata by choosing
Based on a bug in the Web Dynpro Java Runtime you cannot easily retrieve the deployed web
resource sap.jpg by invoking the WDWebResource-API:
WDWebResource.getWebResource(). With this more simple approach a deployed web
resource can be retrieved without creating a file input stream for a resource path. The bug will
be fixed within NW04s SP Stack 12.
FileUpDownloadApp and
Result
Test your application. To do this, navigate to the File Download view by choosing the File Download link.
This displays a download link that allows you to download the image file from the example application.
In addition you can dynamically set the behavior property of the FileDownload-UI-element. The enumeration
type WDFileDownloadBehaviour determines how the downloaded file is represented on the client. This
property can have three different values:
AUTO: The behaviour is predefined and depends on the mime type of the downloaded file.
OPEN_INPLACE: The file will be opened in place in the web page with the browser-embedded
application program.
Graphic 4: Downloading an image file with the file download behavior AUTO
Related Content
SDN Article: Uploading and Downloading Files in Web Dynpro Tables SAP NetWeaver 04s
SAP Online Help: File Upload and File Download
Copyright
Copyright 2006 SAP AG. All rights reserved.
No part of this publication may be reproduced or transmitted in any form or for any purpose without the
express permission of SAP AG. The information contained herein may be changed without prior notice.
Some software products marketed by SAP AG and its distributors contain proprietary software components
of other software vendors.
Microsoft, Windows, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation.
IBM, DB2, DB2 Universal Database, OS/2, Parallel Sysplex, MVS/ESA, AIX, S/390, AS/400, OS/390,
OS/400, iSeries, pSeries, xSeries, zSeries, z/OS, AFP, Intelligent Miner, WebSphere, Netfinity, Tivoli,
Informix, i5/OS, POWER, POWER5, OpenPower and PowerPC are trademarks or registered trademarks of
IBM Corporation.
Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or registered trademarks of
Adobe Systems Incorporated in the United States and/or other countries.
Oracle is a registered trademark of Oracle Corporation.
UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group.
Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or
registered trademarks of Citrix Systems, Inc.
HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C, World Wide Web
Consortium, Massachusetts Institute of Technology.
Java is a registered trademark of Sun Microsystems, Inc.
JavaScript is a registered trademark of Sun Microsystems, Inc., used under license for technology invented
and implemented by Netscape.
MaxDB is a trademark of MySQL AB, Sweden.
SAP, R/3, mySAP, mySAP.com, xApps, xApp, SAP NetWeaver, and other SAP products and services
mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in
Germany and in several other countries all over the world. All other product and service names mentioned
are the trademarks of their respective companies. Data contained in this document serves informational
purposes only. National product specifications may vary.
These materials are subject to change without notice. These materials are provided by SAP AG and its
affiliated companies ("SAP Group") for informational purposes only, without representation or warranty of any
kind, and SAP Group shall not be liable for errors or omissions with respect to the materials. The only
warranties for SAP Group products and services are those that are set forth in the express warranty
statements accompanying such products and services, if any. Nothing herein should be construed as
constituting an additional warranty.
These materials are provided as is without a warranty of any kind, either express or implied, including but
not limited to, the implied warranties of merchantability, fitness for a particular purpose, or non-infringement.
SAP shall not be liable for damages of any kind including without limitation direct, special, indirect, or
consequential damages that may result from the use of these materials.
SAP does not warrant the accuracy or completeness of the information, text, graphics, links or other items
contained within these materials. SAP has no control over the information that you may access through the
use of hot links contained in these materials and does not endorse your use of third party web pages nor
provide any warranty whatsoever relating to third party web pages.
Any software coding and/or code lines/strings (Code) included in this documentation are only examples and
are not intended to be used in a productive system environment. The Code is only intended better explain
and visualize the syntax and phrasing rules of certain coding. SAP does not warrant the correctness and
completeness of the Code given herein, and SAP shall not be liable for errors or damages caused by the
usage of the Code, except if such damages were caused by SAP intentionally or grossly negligent.