Tip

Managing multi-part form data

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

    Requires Free Membership to View

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 austin@opensourceguru.com. Check out his upcoming book Enterprise Java for SAP


This was first published in January 2003

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.