Every so often when building a Web application for SAP, you need to allow the user to upload and store a file on...
a remote file system. Depending on the requirement, you have several approaches to grabbing this file from the user. A simple, but effective one is that of using the standard HTML input tag to provide a file browser window and upload submit.
This tip demonstrates how to build a file upload tool in SAP using the BSP development workbench.
The first step is to create the HTML upload form:
<form action="myupload.htm" enctype="multipart/form-data" method="POST"> <input type="file" name="upload"> <input type="submit" name="submit" value="Upload"> </form>
This creates a text dialog box, a browse button, and a submit button. The form action attribute must be set to the next BSP Web page that will handle the actual binary upload.
The next step is to use an event handler to retrieve the multipart form data. Add the following code to the onInitialization handler in the BSP:
data: multipart_form type ref to if_http_entity, file_upload type xstring. CALL METHOD request->get_multipart EXPORTING INDEX = 1 RECEIVING ENTITY = multipart_form. CALL METHOD multipart_form->get_data RECEIVING DATA = file_upload.
The first method call is GET_MULTIPART, which retrieves the multipart segment based on the index value passed to it. Typically this value is 1, but you may need to use the NUM_MULTIPARTS method to determine which segment is required.
The next method call retrieves the raw, binary data from the multipart segment and sets it to an XSTRING type variable.
Finally, you need to transfer the raw binary to an external dataset:
data dsn(20) value '/tmp/somefile'. open dataset dsn for output in binary mode. transfer file_upload to dsn. close dataset dsn.
This opens a dataset based on the file and directory location set in the DSN variable. Of course, the file extension must match that of the inbound binary. If the file extension is not static, you must determine the file type by looking at the HTTP message body.
The dataset must be opened for output in binary mode. You can then transfer the raw binary to the dataset and close the dataset. That file should now be available on the remote file system, in this case the R/3 application server.
Obviously, this code should be refined to write files to the appropriate system on the network. There are also other mechanisms for writing the file out, such as the GUI_UPLOAD function module.
Author Austin Sincock is a freelance Java/SAP consultant who contributes regularly to Web and print journals. He can be reached at [email protected]. Check out his upcoming book Enterprise Java for SAP