如何将文本写入本地文件
我正在尝试使用以下代码将文本写入本地文件(即在我的笔记本电脑上):
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'.
程序运行良好,执行后出现“完成”。但是,我在 PC 上找不到文本文件“myfile.txt”(它不在 SAP 工作目录中)。
其他信息
我已经使用功能模块GUI_DOWNLOAD
得到了这个工作,但是我必须使用开放数据集 和 TRANSFER 语句,因为我在后台程序中编写此代码(由 BSP< /a> 使用 提交
)。
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
).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在后台处理中不可能写入客户端,因为后台处理的本质是客户端计算机不必连接到 WAS。默认情况下,所有文件都保存到服务器目录
DIR_HOME
中。解决方案:
一般来说,下载数据是通过设置正确的 HTTP 标头字段并借助 cl_bsp_utility=>download 将二进制数据推送到 http 响应中来实现的。
此类在您的响应中设置正确的内容标题。
您必须以
XSTRING
形式指定数据,并指定您想要的Content-Type
和Content-Disposition
,例如application/ vnd.ms-excel
或application/octetstream
。还可以使用
Content-Disposition
告诉浏览器要使用的默认文件名,请参阅PS
有关在 ABAP 中使用文件的一般信息,您可以参考此帮助文件 http://wiki.sdn.sap.com/wiki/display/ABAP/Working+with+files
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 whichContent-Type
andContent-Disposition
you want, for exampleapplication/vnd.ms-excel
orapplication/octetstream
.Also
Content-Disposition
can be used to tell the browser the default filename to use, seeP.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
OPEN DATASET 和相关关键字仅对服务器上的文件进行操作。还有各种其他选项可以将文件从后台进程传输到目标计算机。
如果您的服务器和客户端计算机在 Windows 上运行,您可以在 AL11 中映射 Windows 路径并将文件保存在那里。 (请注意,您必须打开目标 Windows 计算机防火墙才能允许此流量)。
您可以在目标计算机上运行 FTP 服务器并在其中传输文件。 SAP 有功能模块来处理这个问题。查看功能组 SFTP。
如果两台计算机都运行某个版本的 UNIX,您甚至可以 SCP 将文件复制到目标机器(因为大多数 Unix 将包含 ssh,因此包含 scp)。您必须在 SM49 中创建一个外部命令,然后设置从服务器到目标计算机的公钥身份验证,如果您没有 SAP 服务器的管理员权限,这会有点棘手,但有一些方法可以解决这个问题
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.
正如 Turismo 所写: 在后台处理中,您无法在 PC 上写入。
写入 PC 需要连接 SAPGui(=客户端)。
OPEN DATASET
和TRANSFER
将数据写入服务器。As Turismo wrote: In Background processing, you can not write on a PC.
Writing to a PC needs a connected SAPGui (=Client).
OPEN DATASET
andTRANSFER
writes the data to the server.