on 04-27-2009 11:43 AM
Hello,
We have a requirement on which we want to download a PDF file stored in Application server using BSP application. I have used function module ARCHIVFILE_SERVER_TO_CLIENT but this FM will help only if i want to download file from GUI it won't work in BSP application.
Further i have used class CL_BSP_UTILITY and download method to download file from application server but it is not working in desired manner.
I am attaching my code for your reference:
DATA: BUTTON_EVENT TYPE REF TO CL_HTMLB_EVENT_BUTTON ,
EVENT TYPE REF TO IF_HTMLB_DATA.
DATA: LS_HOURS LIKE LINE OF GT_HOURS.
DATA STR TYPE STRING.
DATA: OUTPUT TYPE STRING ,
L_XSTRING TYPE XSTRING ,
APP_TYPE TYPE STRING.
EVENT = CL_HTMLB_MANAGER=>GET_EVENT( REQUEST ).
DATA PHY_NAME_OUT TYPE SAPB-SAPPFAD.
IF EVENT IS NOT INITIAL AND EVENT->EVENT_NAME = HTMLB_EVENTS=>BUTTON .
BUTTON_EVENT ?= EVENT .
CASE EVENT->EVENT_SERVER_NAME.
WHEN 'test' .
IF GT_HOURS IS NOT INITIAL.
PHY_NAME_OUT = '/usr/sap/put/form16//01000200_2007.PDF'.
OPEN DATASET PHY_NAME_OUT FOR INPUT IN TEXT MODE ENCODING DEFAULT.
IF SY-SUBRC IS INITIAL.
DO.
READ DATASET PHY_NAME_OUT INTO STR.
IF SY-SUBRC IS INITIAL.
CONCATENATE
OUTPUT
str
cl_abap_char_utilities=>cr_lf
INTO output SEPARATED BY space. "cl_abap_char_utilities=>horizontal_tab
ELSE.
EXIT.
ENDIF.
ENDDO.
ENDIF.
*LOOP AT gt_hours INTO ls_hours.
*
*CONCATENATE
*
*OUTPUT
*
*ls_hours-hour
*
*cl_abap_char_utilities=>cr_lf
*
*INTO output SEPARATED BY space. "cl_abap_char_utilities=>horizontal_tab
* .
*
*.
*
*
*
*ENDLOOP.
APP_TYPE = 'APPLICATION/PDF;charset=utf-16le'.
*
CALL FUNCTION 'SCMS_STRING_TO_XSTRING'
EXPORTING
TEXT = OUTPUT
MIMETYPE = 'APPLICATION/PDF;charset=utf-16le'
IMPORTING
BUFFER = L_XSTRING.
CONCATENATE CL_ABAP_CHAR_UTILITIES=>BYTE_ORDER_MARK_LITTLE
L_XSTRING
INTO L_XSTRING IN BYTE MODE.
CALL METHOD CL_BSP_UTILITY=>DOWNLOAD
EXPORTING
OBJECT_S = L_XSTRING
CONTENT_TYPE = APP_TYPE
CONTENT_DISPOSITION = 'attachment;filename=webforms.pdf'
RESPONSE = _M_RESPONSE
NAVIGATION = NAVIGATION.
ENDIF.
ENDCASE.
ENDIF.
From this code i am able to download PDF file but it is not opening in local machine.
If any other way to download file then please suggest.
waiting for ur reply.
Regards,
Gagan
Hi,
you do the file reading wrong:
have a look at:
* Local declarations.
data:
components type stringtab,
component type string,
path type text255,
file type text255,
line_length type i,
_file_content_binary type sdokcntbin,
rows type sytabix,
content type sdokcntbin,
exception type ref to cx_sy_file_access_error,
exception_tmf type ref to cx_sy_too_many_files ,
block_size type i value 1022,
length type i.
field-symbols:
<hex_container> type x.
constants:
c_dms_blk_size type i value 2550.
clear file_size.
try.
open dataset file_name for input in binary mode message message.
catch cx_sy_file_open into exception.
case exception->textid.
when '4182174D03030063000000000A1551B1'. raise access_error.
when '41825AD355C3005E000000000A1551B1'. raise open_error.
when '47E8B03AECE5BA07E10000000A114829'. raise already_open.
when others.
endcase.
catch cx_sy_file_authority into exception.
case exception->textid.
when '4182174D03030063000000000A1551B1'. raise access_error.
when 'A70BB8396F051547E10000000A11447B'. raise authority_error.
when others.
endcase.
catch cx_sy_too_many_files into exception_tmf.
case exception_tmf->textid.
when '8708B73915F6B645E10000000A11447B'. raise too_many_files.
when others.
endcase.
endtry.
do.
try.
read dataset file_name into _file_content_binary-line.
if sy-subrc <> 0.
add line_length to file_size.
append _file_content_binary to file_content_binary.
exit.
else.
add line_length to file_size.
append _file_content_binary to file_content_binary.
endif.
catch cx_sy_file_open_mode into exception.
case exception->textid.
when '9207B73915F6B645E10000000A11447B'. raise cx_sy_file_open_mode.
when '9807B73915F6B645E10000000A11447B'. raise read_only.
when '9E07B73915F6B645E10000000A11447B'. raise not_open.
when '409D273A2D824360E10000000A11447B'. raise incompatible_mode.
when others.
endcase.
endtry.
enddo.
try.
close dataset file_name.
catch cx_sy_file_close into exception.
case exception->textid.
when '4182174D03030063000000000A1551B1'. raise access_error.
when 'C10BB8396F051547E10000000A11447B'. raise close_error.
when others.
endcase.
catch cx_sy_file_access_error into exception.
case exception->textid.
when '4182174D03030063000000000A1551B1'. raise access_error.
when others.
endcase.
endtry.
* Get file size.
split file_name at '/' into table components.
describe table components lines sy-tfill.
read table components into component index sy-tfill.
path = file_name.
replace component in path with ''.
file = component.
call function '/EUH/MMS_GET_FILE_SIZE'
exporting
dir_name = path
file_name = file
importing
file_size = file_size.
and function /euh/mms_get_file_size.
*"----------------------------------------------------------------------
*"*"Local Interface:
*" IMPORTING
*" VALUE(DIR_NAME) TYPE TEXT255
*" VALUE(FILE_NAME) TYPE TEXT255
*" EXPORTING
*" VALUE(FILE_SIZE) TYPE SDOK_FSIZE
*" EXCEPTIONS
*" NO_AUTHORITY
*" ACTIVITY_UNKNOWN
*" NOT_A_DIRECTORY
*" NO_MEDIA_IN_DRIVE
*" TOO_MANY_ERRORS
*" TOO_MANY_FILES
*" BRACKET_ERROR_IN_FILENAME
*" NO_SUCH_PARAMETER
*"----------------------------------------------------------------------
types: begin of files,
line like ocs_file,
end of files.
data: file type files,
f_subrc like sy-subrc value 0,
errno(3) type c,
errmsg(40) type c,
pos type i,
len type i,
parameter(120) type c,
help1(120) type c,
help2(120) type c,
error_counter type i.
data:
dir_list like ocs_file occurs 0 with header line.
* CASE sy-subrc.
* WHEN 1. f_subrc = 64.
* WHEN 2. f_subrc = 65.
* WHEN 0.
search dir_name for 'SY-HOST'. "Is SY-HOST used in directory name?
if sy-subrc = 0.
pos = sy-fdpos + 7.
move dir_name(sy-fdpos) to help1.
move dir_name+pos to help2.
concatenate help1 sy-host help2 into dir_name.
endif.
do 12 times. " To avoid endless loop
if dir_name cs '$('. " Replace parameter by their value
pos = sy-fdpos + 2.
if dir_name cs ')'.
len = sy-fdpos - pos.
if len le 0.
f_subrc = 4. exit.
else.
parameter = dir_name+pos(len).
call 'C_SAPGPARAM' id 'NAME' field parameter
id 'VALUE' field parameter.
if sy-subrc = 0.
len = pos + len + 1.
pos = pos - 2.
if pos > 0.
move dir_name(pos) to help1.
else.
help1 = ''.
endif.
pos = strlen( dir_name ).
if pos > len.
move dir_name+len to help2.
else.
help2 = ''.
endif.
concatenate help1 parameter help2 into dir_name.
else.
f_subrc = 8. exit.
endif.
endif.
else.
exit.
endif.
else.
exit.
endif.
enddo.
shift dir_name right deleting trailing '/\ '.
shift dir_name left deleting leading space.
call 'C_DIR_READ_FINISH' " this is from RSWATCH0
id 'ERRNO' field errno
id 'ERRMSG' field errmsg.
call 'C_DIR_READ_START'
id 'DIR' field dir_name
id 'FILE' field file_name
id 'ERRNO' field errno
id 'ERRMSG' field errmsg.
case sy-subrc.
when 1.
case errno.
when 2 or 20. f_subrc = f_subrc + 66.
when ' '. f_subrc = f_subrc + 67.
endcase.
when 0.
clear error_counter.
do.
clear file.
clear dir_list.
call 'C_DIR_READ_NEXT'
id 'TYPE' field file-line-type
id 'NAME' field file-line-name
id 'LEN' field file-line-len
id 'OWNER' field file-line-owner
id 'MTIME' field file-line-mtime
id 'MODE' field file-line-acc_mode
id 'ERRNO' field errno
id 'ERRMSG' field errmsg. "sy-subrc = 3 is e.g. if return
if sy-subrc = 0 or sy-subrc = 3."data do not fit into variables
perform p6_to_date_time_tz(rstr0400) using
file-line-mtime
file-line-mod_time
file-line-mod_date.
file_size = file-line-len.
dir_list = file-line.
append dir_list.
elseif sy-subrc = 1. " nothing (more) found
exit.
else.
if error_counter > 50.
call 'C_DIR_READ_FINISH'
id 'ERRNO' field errno
id 'ERRMSG' field errmsg.
pos = f_subrc mod 2.
if pos = 0. f_subrc = f_subrc + 1. endif.
exit.
endif.
add 1 to error_counter.
endif.
if sy-index > 1000.
pos = f_subrc mod 4.
if pos = 0. f_subrc = f_subrc + 2. endif.
exit.
endif.
enddo.
call 'C_DIR_READ_FINISH'
id 'ERRNO' field errno
id 'ERRMSG' field errmsg.
if sy-opsys(3) = 'Win' and not ( file_name cs '*' ).
describe table dir_list lines len.
if len = 0.
help1 = file_name.
translate help1 to upper case.
translate help1 using
'A+B+C+D+E+F+G+H+I+J+K+L+M+N+O+P+Q+R+S+T+U+V+W+X+Y+Z+'.
call 'C_DIR_READ_START'
id 'DIR' field dir_name
id 'FILE' field help1.
if sy-subrc = 0.
clear error_counter.
do.
clear file.
clear dir_list.
call 'C_DIR_READ_NEXT'
id 'TYPE' field file-line-type
id 'NAME' field file-line-name
id 'LEN' field file-line-len
id 'OWNER' field file-line-owner
id 'MTIME' field file-line-mtime
id 'MODE' field file-line-acc_mode
id 'ERRNO' field errno
id 'ERRMSG' field errmsg.
if sy-subrc = 0 or sy-subrc = 3.
help2 = file_name.
translate help2 to upper case.
help1 = file-line-name.
translate help1 to upper case.
if help1 = help2.
perform p6_to_date_time_tz(rstr0400) using
file-line-mtime
file-line-mod_time
file-line-mod_date.
dir_list = file-line.
append dir_list.
endif.
elseif sy-subrc = 1.
exit.
else.
if error_counter > 50.
call 'C_DIR_READ_FINISH'
id 'ERRNO' field errno
id 'ERRMSG' field errmsg.
pos = f_subrc mod 2.
if pos = 0. f_subrc = f_subrc + 1. endif.
exit.
endif.
add 1 to error_counter.
endif.
if sy-index > 1000.
pos = f_subrc mod 4.
if pos = 0. f_subrc = f_subrc + 2. endif.
exit.
endif.
enddo.
call 'C_DIR_READ_FINISH'
id 'ERRNO' field errno
id 'ERRMSG' field errmsg.
endif.
endif.
endif.
endcase.
* ENDCASE.
* CASE f_subrc.
* WHEN 1 OR 3. RAISE too_many_errors.
* WHEN 2. RAISE too_many_files.
* WHEN 4 OR 5 OR 6 OR 7 OR 70 OR 71.
* RAISE bracket_error_in_filename.
* WHEN 8 OR 9 OR 10 OR 11 OR 74 OR 75.
* RAISE no_such_parameter.
* WHEN 64. RAISE no_authority.
* WHEN 65. RAISE activity_unknown.
* WHEN 66. RAISE not_a_directory.
* WHEN 67. RAISE no_media_in_drive.
* ENDCASE.
endfunction.
Now you will be able to get the filesize and the file in SDOK format sdokbin.
Okay.
You have to build an request and push the content to the user like:
...
call method server->response->set_header_field( name = 'Content-Type' value = value ).
describe field file_content_binary-line length line_length in byte mode.
bytes_rest = file-size.
loop at file_content_binaries assigning <file_content_binary>.
move <file_content_binary>-line to data.
if bytes_rest <= 0. "mismatch between line values and size
exit.
endif.
if bytes_rest >= line_length.
call method server->response->append_data( data = data
length = line_length ).
else.
call method server->response->append_data( data = data
length = bytes_rest ).
endif.
bytes_rest = bytes_rest - line_length.
endloop.
....
This will work.
You can set the header fields like you want. Have also a look at sicf node contentserver
and its handler: CL_HTTP_EXT_CSIF
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
you do the file reading wrong:
have a look at:
Local declarations.
data:
components type stringtab,
component type string,
path type text255,
file type text255,
line_length type i,
filecontent_binary type sdokcntbin,
rows type sytabix,
content type sdokcntbin,
exception type ref to cx_sy_file_access_error,
exception_tmf type ref to cx_sy_too_many_files ,
block_size type i value 1022,
length type i.
field-symbols:
<hex_container> type x.
constants:
c_dms_blk_size type i value 2550.
clear file_size.
try.
open dataset file_name for input in binary mode message message.
catch cx_sy_file_open into exception.
case exception->textid.
when '4182174D03030063000000000A1551B1'. raise access_error.
when '41825AD355C3005E000000000A1551B1'. raise open_error.
when '47E8B03AECE5BA07E10000000A114829'. raise already_open.
when others.
endcase.
catch cx_sy_file_authority into exception.
case exception->textid.
when '4182174D03030063000000000A1551B1'. raise access_error.
when 'A70BB8396F051547E10000000A11447B'. raise authority_error.
when others.
endcase.
catch cx_sy_too_many_files into exception_tmf.
case exception_tmf->textid.
when '8708B73915F6B645E10000000A11447B'. raise too_many_files.
when others.
endcase.
endtry.
do.
try.
read dataset file_name into filecontent_binary-line.
if sy-subrc <> 0.
add line_length to file_size.
append filecontent_binary to file_content_binary.
exit.
else.
add line_length to file_size.
append filecontent_binary to file_content_binary.
endif.
catch cx_sy_file_open_mode into exception.
case exception->textid.
when '9207B73915F6B645E10000000A11447B'. raise cx_sy_file_open_mode.
when '9807B73915F6B645E10000000A11447B'. raise read_only.
when '9E07B73915F6B645E10000000A11447B'. raise not_open.
when '409D273A2D824360E10000000A11447B'. raise incompatible_mode.
when others.
endcase.
endtry.
enddo.
try.
close dataset file_name.
catch cx_sy_file_close into exception.
case exception->textid.
when '4182174D03030063000000000A1551B1'. raise access_error.
when 'C10BB8396F051547E10000000A11447B'. raise close_error.
when others.
endcase.
catch cx_sy_file_access_error into exception.
case exception->textid.
when '4182174D03030063000000000A1551B1'. raise access_error.
when others.
endcase.
endtry.
Get file size.
split file_name at '/' into table components.
describe table components lines sy-tfill.
read table components into component index sy-tfill.
path = file_name.
replace component in path with ''.
file = component.
call function '/EUH/MMS_GET_FILE_SIZE'
exporting
dir_name = path
file_name = file
importing
file_size = file_size.
and function /euh/mms_get_file_size.
*"----
""Local Interface:
*" IMPORTING
*" VALUE(DIR_NAME) TYPE TEXT255
*" VALUE(FILE_NAME) TYPE TEXT255
*" EXPORTING
*" VALUE(FILE_SIZE) TYPE SDOK_FSIZE
*" EXCEPTIONS
*" NO_AUTHORITY
*" ACTIVITY_UNKNOWN
*" NOT_A_DIRECTORY
*" NO_MEDIA_IN_DRIVE
*" TOO_MANY_ERRORS
*" TOO_MANY_FILES
*" BRACKET_ERROR_IN_FILENAME
*" NO_SUCH_PARAMETER
*"----
types: begin of files,
line like ocs_file,
end of files.
data: file type files,
f_subrc like sy-subrc value 0,
errno(3) type c,
errmsg(40) type c,
pos type i,
len type i,
parameter(120) type c,
help1(120) type c,
help2(120) type c,
error_counter type i.
data:
dir_list like ocs_file occurs 0 with header line.
CASE sy-subrc.
WHEN 1. f_subrc = 64.
WHEN 2. f_subrc = 65.
WHEN 0.
search dir_name for 'SY-HOST'. "Is SY-HOST used in directory name?
if sy-subrc = 0.
pos = sy-fdpos + 7.
move dir_name(sy-fdpos) to help1.
move dir_name+pos to help2.
concatenate help1 sy-host help2 into dir_name.
endif.
do 12 times. " To avoid endless loop
if dir_name cs '$('. " Replace parameter by their value
pos = sy-fdpos + 2.
if dir_name cs ')'.
len = sy-fdpos - pos.
if len le 0.
f_subrc = 4. exit.
else.
parameter = dir_name+pos(len).
call 'C_SAPGPARAM' id 'NAME' field parameter
id 'VALUE' field parameter.
if sy-subrc = 0.
len = pos + len + 1.
pos = pos - 2.
if pos > 0.
move dir_name(pos) to help1.
else.
help1 = ''.
endif.
pos = strlen( dir_name ).
if pos > len.
move dir_name+len to help2.
else.
help2 = ''.
endif.
concatenate help1 parameter help2 into dir_name.
else.
f_subrc = 8. exit.
endif.
endif.
else.
exit.
endif.
else.
exit.
endif.
enddo.
shift dir_name right deleting trailing '/\ '.
shift dir_name left deleting leading space.
call 'C_DIR_READ_FINISH' " this is from RSWATCH0
id 'ERRNO' field errno
id 'ERRMSG' field errmsg.
call 'C_DIR_READ_START'
id 'DIR' field dir_name
id 'FILE' field file_name
id 'ERRNO' field errno
id 'ERRMSG' field errmsg.
case sy-subrc.
when 1.
case errno.
when 2 or 20. f_subrc = f_subrc + 66.
when ' '. f_subrc = f_subrc + 67.
endcase.
when 0.
clear error_counter.
do.
clear file.
clear dir_list.
call 'C_DIR_READ_NEXT'
id 'TYPE' field file-line-type
id 'NAME' field file-line-name
id 'LEN' field file-line-len
id 'OWNER' field file-line-owner
id 'MTIME' field file-line-mtime
id 'MODE' field file-line-acc_mode
id 'ERRNO' field errno
id 'ERRMSG' field errmsg. "sy-subrc = 3 is e.g. if return
if sy-subrc = 0 or sy-subrc = 3."data do not fit into variables
perform p6_to_date_time_tz(rstr0400) using
file-line-mtime
file-line-mod_time
file-line-mod_date.
file_size = file-line-len.
dir_list = file-line.
append dir_list.
elseif sy-subrc = 1. " nothing (more) found
exit.
else.
if error_counter > 50.
call 'C_DIR_READ_FINISH'
id 'ERRNO' field errno
id 'ERRMSG' field errmsg.
pos = f_subrc mod 2.
if pos = 0. f_subrc = f_subrc + 1. endif.
exit.
endif.
add 1 to error_counter.
endif.
if sy-index > 1000.
pos = f_subrc mod 4.
if pos = 0. f_subrc = f_subrc + 2. endif.
exit.
endif.
enddo.
call 'C_DIR_READ_FINISH'
id 'ERRNO' field errno
id 'ERRMSG' field errmsg.
if sy-opsys(3) = 'Win' and not ( file_name cs '*' ).
describe table dir_list lines len.
if len = 0.
help1 = file_name.
translate help1 to upper case.
translate help1 using
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
call 'C_DIR_READ_START'
id 'DIR' field dir_name
id 'FILE' field help1.
if sy-subrc = 0.
clear error_counter.
do.
clear file.
clear dir_list.
call 'C_DIR_READ_NEXT'
id 'TYPE' field file-line-type
id 'NAME' field file-line-name
id 'LEN' field file-line-len
id 'OWNER' field file-line-owner
id 'MTIME' field file-line-mtime
id 'MODE' field file-line-acc_mode
id 'ERRNO' field errno
id 'ERRMSG' field errmsg.
if sy-subrc = 0 or sy-subrc = 3.
help2 = file_name.
translate help2 to upper case.
help1 = file-line-name.
translate help1 to upper case.
if help1 = help2.
perform p6_to_date_time_tz(rstr0400) using
file-line-mtime
file-line-mod_time
file-line-mod_date.
dir_list = file-line.
append dir_list.
endif.
elseif sy-subrc = 1.
exit.
else.
if error_counter > 50.
call 'C_DIR_READ_FINISH'
id 'ERRNO' field errno
id 'ERRMSG' field errmsg.
pos = f_subrc mod 2.
if pos = 0. f_subrc = f_subrc + 1. endif.
exit.
endif.
add 1 to error_counter.
endif.
if sy-index > 1000.
pos = f_subrc mod 4.
if pos = 0. f_subrc = f_subrc + 2. endif.
exit.
endif.
enddo.
call 'C_DIR_READ_FINISH'
id 'ERRNO' field errno
id 'ERRMSG' field errmsg.
endif.
endif.
endif.
endcase.
ENDCASE.
CASE f_subrc.
WHEN 1 OR 3. RAISE too_many_errors.
WHEN 2. RAISE too_many_files.
WHEN 4 OR 5 OR 6 OR 7 OR 70 OR 71.
RAISE bracket_error_in_filename.
WHEN 8 OR 9 OR 10 OR 11 OR 74 OR 75.
RAISE no_such_parameter.
WHEN 64. RAISE no_authority.
WHEN 65. RAISE activity_unknown.
WHEN 66. RAISE not_a_directory.
WHEN 67. RAISE no_media_in_drive.
ENDCASE.
endfunction.
Now you will be able to get the filesize and the file in SDOK format sdokbin.
Okay.
You have to build an request and push the content to the user like:
...
call method server->response->set_header_field( name = 'Content-Type' value = value ).
describe field file_content_binary-line length line_length in byte mode.
bytes_rest = file-size.
loop at file_content_binaries assigning <file_content_binary>.
move <file_content_binary>-line to data.
if bytes_rest <= 0. "mismatch between line values and size
exit.
endif.
if bytes_rest >= line_length.
call method server->response->append_data( data = data
length = line_length ).
else.
call method server->response->append_data( data = data
length = bytes_rest ).
endif.
bytes_rest = bytes_rest - line_length.
endloop.
....
This will work.
You can set the header fields like you want. Have also a look at sicf node contentserver
and its handler: CL_HTTP_EXT_CSIF
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi,
Check out this blog:
[BSP/HowTo: Generate PDF Output from a BSP.|https://www.sdn.sap.com/irj/scn/weblogs?blog=/pub/wlg/1002] [original link is broken] [original link is broken] [original link is broken];
Regards,
Anubhav
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
101 | |
13 | |
13 | |
11 | |
11 | |
7 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.