在 HTTP 响应中发送多个文件 [英] Send multiple files in HTTP response

查看:39
本文介绍了在 HTTP 响应中发送多个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个 ICF 处理程序类,用于将文件发送给发件人.问题是,它适用于单个文件,我以二进制格式读取数据并使用 set_data 在正文部分附加相同的数据.

I have created an ICF handler class which sends files to the sender. The thing is, it works fine with single file where i am reading the data in binary format and attaching the same in body part using set_data.

但是当我尝试添加 1 个以上的文件时,我无法分别添加 2 个文件.我正在使用 IF_HTTP_EXTENSION 并且还没有 NTW GATEWAY 组件.

But when I try to add more than 1 file, I am unable to add 2 files separately. i am using IF_HTTP_EXTENSION and do not have NTW GATEWAY component yet.

我也在使用 MULTIPART 功能,但不知道如何分别添加 2 个文件.你能帮我吗?

I am also using MULTIPART feature, but dont konw exactly on how to add 2 files separately. Can you please help me ?

//file1
server->response->set_header_field( name = 'Content-Type' value = 'multipart/mixed').
CONCATENATE 'form-data;name="file"; filename="' filename+5(9) '"' INTO lv_header_value.
server->response->set_header_field( name = 'content-disposition' value = lv_header_value ).
server->response->set_data( data = attach_xstring ).

//file2
server->response->add_multipart( ).
CONCATENATE 'form-data;name="file"; filename="' filename+5(9) '"' INTO lv_header_value.
server->response->set_header_field( name = 'content-disposition' value = lv_header_value ).
server->response->set_data( data = attach_xstring ).

推荐答案

您需要使用 add_multipart() 方法.试试这个:

You need to use add_multipart() method. Try like this:

      cl_http_client=>create( EXPORTING host = host service = port scheme = scheme
                              IMPORTING client = lo_http_client ).
      lo_http_client->request->set_header_field( name  = 'Content-Type' value = 'multipart/form-data' ). "#EC NOTEXT
      lo_request_part = lo_http_client->request->add_multipart( ).
      lo_request_part->set_content_type( 'application/xml' ).
      lv_content_disposition = |form-data; name="item"; filename="item_data.xml" |.
      lo_request_part->set_header_field( name = `Content-Disposition` value = lv_content_disposition ).
      lo_request_part->set_data( data = lv_create_item_xml ).

      LOOP AT mt_files ASSIGNING <attachment>.
        lo_request_part = lo_http_client->request->add_multipart( ).
        lo_request_part->set_content_type( <attachment>-content_type ). "#EC NOTEXT
        lv_content_disposition =  |form-data; name="{ <attachment>-part_name }"; filename="{ <attachment>-filename }" |.
        lo_request_part->set_header_field( name = `Content-Disposition` value = lv_content_disposition ).
        lo_request_part->set_data( <attachment>-file ).
      ENDLOOP.

这是请求的示例,但对于响应,方案应该是相同的.这里最初将 xml 文件添加到请求中,并且循环处理它们的多个附件.

It is sample for request, but for response the scheme should be the same. Here initially xml-file added to request and them multiple attachments are processed in loop.

这篇关于在 HTTP 响应中发送多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆