在HTTP响应中发送多个文件

在HTTP响应中发送多个文件

本文介绍了在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.

但是当我尝试添加多个文件时,无法分别添加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响应中发送多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 13:24