How to write text to a local file

B. Bowles picture B. Bowles · Jan 19, 2012 · Viewed 19.1k times · Source

I'm trying to write text to a local file (i.e. on my laptop) using the following code:

data: fname(60), text type string value 'la la la'.
fname = 'myfile.txt'.
OPEN DATASET fname FOR OUTPUT IN TEXT MODE encoding default.
TRANSFER text TO fname.
CLOSE DATASET fname.
write 'done'.

The program runs fine and "done" appears after execution. However I cannot find the text file "myfile.txt" on my PC (it's not in the SAP work directory).

Additional Info

I have gotten this working using the function module GUI_DOWNLOAD, however I have to use the OPEN DATASET and TRANSFER statements as I'm writing this in a background program (to be called by a BSP using SUBMIT).

Answer

Turismo picture Turismo · Jan 19, 2012

It is not possible to write to a client while in background processing because nature of background processing is that no client machine has to be connected to the WAS. By default all files are save into sever directory DIR_HOME.

SOLUTION: Generally downloading data is achieved by setting the right HTTP header fields and pushing the binary data into the http response with the help of cl_bsp_utility=>download.

This class sets the right content headers in your response. You have to specify your data in XSTRING form and specify which Content-Type and Content-Disposition you want, for example application/vnd.ms-excel or application/octetstream.

Also Content-Disposition can be used to tell the browser the default filename to use, see

attachment; filename=filexyz.xls`

P.S. For general information on working with files in ABAP you can refer to this help file http://wiki.sdn.sap.com/wiki/display/ABAP/Working+with+files