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
).
Steps for writing to text files First, open the text file for writing (or append) using the open() function. Second, write to the text file using the write() or writelines() method. Third, close the file using the close() method.
Windows 10 Open File Explorer and navigate to the folder where you want to create the text file. Right-click in the folder and go to New > Text Document. The text file is given a default name, New Text Document. txt, but the file name is highlighted.
AppendText(String) Method (System.IO) Creates a StreamWriter that appends UTF-8 encoded text to an existing file, or to a new file if the specified file does not exist.
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
The OPEN DATASET and related keywords operate on files on the server only. There are various other options for getting the file on to the target machine from a background process.
If your server and client machines run on Windows, you can map a Windows path in AL11 and save the file there. (Note that you will have to open the target Windows machine firewall to allow this traffic).
You can run an FTP server on the target machine and ftp the files there. SAP has function modules to deal with this. Look at function group SFTP.
If both machines run some version of UNIX, you can even SCP the files on to the target machine (as most Unixes will include ssh and therefore scp). You will have to create an external command in SM49 and then set up public key authentication from the server to the target machine, which is a bit more tricky if you don't have admin rights on the SAP server, but there are ways around that too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With