Upload/Download any file to a Z table
English: With this blog you will know how to upload/download any file to a Z table.
This blog has arisen due to a question from a colleague in the SCN community.
I am posting a program I did in 2010.
Portuguese: Com esse blog você vai saber como fazer um upload/download de qualquer arquivo para uma tabela Z.
Esse blog surgiu devido a uma pergunta de um colega da comunidade SCN.
Estou postando um programa que fiz em 2010.
Passos:
1) Crie a tabela ZARQUIVO conforme figura abaixo:
Campos:
ID: apenas será um identificador de cada arquivo.
PARTIDX: será um contador de cada arquivo, irei quebrar o tamanho de 1000 em 1000.
FILENAME: será o nome do arquivo/localização
DATA_SIZE: será o tamanho binário de cada linha.
DATA: será o conteúdo binário do arquivo.
2) Crie o programa abaixo:
| ZFILE_ANEXO |
|---|
|
*&———————————————————————* *& Report ZFILE_ANEXO *& *&———————————————————————* *& *& *&———————————————————————* REPORT ZFILE_ANEXO. TYPES : BEGIN OF ty_data, data type x length 1, END OF ty_data. DATA : t_data type standard table of ty_data, w_data like line of t_data. DATA : t_data_tab type standard table of zarquivo, w_data_tab like line of t_data_tab. *Parametros Parameters : p_upload type c radiobutton group rb1, p_downlo type c radiobutton group rb1, p_id type ZARQUIVO-id obligatory, p_file type localfile. AT SELECTION-SCREEN ON VALUE-REQUEST FOR p_file. PERFORM f_f4help_local_fname CHANGING p_file. *inicio start-of-selection. if not p_upload is initial. perform f_upload. else. perform f_download. endif. *Forms *&———————————————————————* *& Form f_upload *&———————————————————————* FORM f_upload. data: l_file type string, l_c1 type i, l_c2 type i, l_data type x length 1000. delete from zarquivo where id = p_id. l_file = p_file. CALL FUNCTION ‘GUI_UPLOAD’ EXPORTING FILENAME = l_file FILETYPE = ‘BIN’ TABLES DATA_TAB = t_data EXCEPTIONS FILE_OPEN_ERROR = 1 FILE_READ_ERROR = 2 NO_BATCH = 3 GUI_REFUSE_FILETRANSFER = 4 INVALID_TYPE = 5 NO_AUTHORITY = 6 UNKNOWN_ERROR = 7 BAD_DATA_FORMAT = 8 HEADER_NOT_ALLOWED = 9 SEPARATOR_NOT_ALLOWED = 10 HEADER_TOO_LONG = 11 UNKNOWN_DP_ERROR = 12 ACCESS_DENIED = 13 DP_OUT_OF_MEMORY = 14 DISK_FULL = 15 DP_TIMEOUT = 16 OTHERS = 17. IF SY-SUBRC <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. DESCRIBE TABLE t_data. CHECK sy-tfill > 0. w_data_tab-id = p_id. w_data_tab-filename = l_file. l_c1 = 0. l_c2 = 0. loop at t_data into w_data. l_data+l_c1 = w_data-data. add 1 to l_c1. if l_c1 = 1000. add 1 to l_c2. w_data_tab-partidx = l_c2. w_data_tab-data_size = l_c1. w_data_tab-data = l_data. append w_data_tab to t_data_tab. clear l_c1. clear l_data. endif. endloop. add 1 to l_c2. w_data_tab-partidx = l_c2. w_data_tab-data_size = l_c1. w_data_tab-data = l_data. append w_data_tab to t_data_tab. modify zarquivo from table t_data_tab. if sy-subrc = 0. *Upload efetuado com sucesso. message s208(00) with text-s01. endif. ENDFORM. ” f_upload *&———————————————————————* *& Form f_download *&———————————————————————* FORM f_download. data : l_file type string, l_c1 type i. l_file = p_file. SELECT * INTO TABLE t_data_tab FROM zarquivo WHERE ID = P_ID. CHECK SY-SUBRC = 0. LOOP AT t_data_tab into w_data_tab. DO w_data_tab-data_size times. l_c1 = sy-index – 1. w_data-data = w_data_tab-data+l_c1. APPEND w_data to t_data. ENDDO. ENDLOOP. DESCRIBE TABLE t_data. CHECK sy-tfill > 0. CALL FUNCTION ‘GUI_DOWNLOAD’ EXPORTING BIN_FILESIZE = sy-tfill FILENAME = l_file FILETYPE = ‘BIN’ TABLES DATA_TAB = t_data EXCEPTIONS FILE_WRITE_ERROR = 1 NO_BATCH = 2 GUI_REFUSE_FILETRANSFER = 3 INVALID_TYPE = 4 NO_AUTHORITY = 5 UNKNOWN_ERROR = 6 HEADER_NOT_ALLOWED = 7 SEPARATOR_NOT_ALLOWED = 8 FILESIZE_NOT_ALLOWED = 9 HEADER_TOO_LONG = 10 DP_ERROR_CREATE = 11 DP_ERROR_SEND = 12 DP_ERROR_WRITE = 13 UNKNOWN_DP_ERROR = 14 ACCESS_DENIED = 15 DP_OUT_OF_MEMORY = 16 DISK_FULL = 17 DP_TIMEOUT = 18 FILE_NOT_FOUND = 19 DATAPROVIDER_EXCEPTION = 20 CONTROL_FLUSH_ERROR = 21 OTHERS = 22. IF SY-SUBRC <> 0. MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ENDIF. ENDFORM. ” f_download *&———————————————————————* *& Form F4HELP_LOCAL_FNAME *&———————————————————————* FORM f_f4help_local_fname CHANGING cv_filename TYPE localfile. DATA: lv_path TYPE dxfields-longpath, lv_abend_flag TYPE dxfields-abendflag. CALL FUNCTION ‘F4_DXFILENAME_TOPRECURSION’ EXPORTING i_location_flag = ‘P’ IMPORTING o_path = lv_path abend_flag = lv_abend_flag EXCEPTIONS rfc_error = 1 error_with_gui = 2 OTHERS = 3. IF sy-subrc <> 0. * MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO * WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4. ELSEIF lv_abend_flag IS INITIAL. cv_filename = lv_path. ENDIF. ENDFORM. ” F_F4HELP_LOCAL_FNAME |
3) Crie os elementos de textos e textos de seleção:
|
Símbolo de Texto |
Texto |
|
S01 |
Upload efetuado com sucesso. |
|
Campo de Seleção |
Texto |
|
P_DOWNLO |
Download |
|
P_FILE |
Arquivo |
|
P_ID |
ID sequencial do arquivo |
|
P_UPLOAD |
Upload |
4) Fazer o UPLOAD do arquivo
Rode o programa criado ZFILE_ANEXO pela transação SE38 ou SA38.
Nesse primeiro momento o programa irá fazer o UPLOAD do arquivo TESTE.DOC para a tabela ZARQUIVO e irá aparecer a mensagem de sucesso: “upload efetuado com sucesso”.
5) Visualize a tabela após o UPLOAD do arquivo
A chave da tabela é o ID (identificador do arquivo) e PARTIDX (partes binárias do identificador do arquivo dividido em tamanhos de 1000).
6) Fazer o DOWNLOAD do arquivo e salvar na máquina local
Realizar o processo inverso. Você pode usar o mesmo nome do arquivo ou até mesmo salvar como outro nome. Neste caso utilizei outro nome TESTE2.DOC
7) Verificar a pasta onde foi feito o download
O arquivo DOWNLOAD foi criado exatamente igual ao original conforme mostrado abaixo:





Regards
Clemens
This blog is published in the language "Portuguese" permitted by SDN. I added a brief introduction in English to the international community can learn what it's about the subject.
I will try to improve next.
Thank you.
The error i am getting while opening the downloaded word document is :
word cannot start the converter mswrd632.
Can you please open the document teste2.doc and confirm whether you are getting same error.
I just did a test download "teste2.doc" and everything went right.
I created a document with only one line and the success message was this:
26,112 Bytes Transferred
Message on. FES028
Please try a different extension, for example: .PDF to see if the same message appears.
Regards
Bruno.
The error i am getting while opening the downloaded word document is :
word cannot start the converter mswrd632."
This is a problem on your local machine, see
http://support.microsoft.com/kb/973904/en-us
谢谢。
我希望,谷歌翻译的正确。
you might want to add "Portuguese" to the list of blog topics, so that it's more clear that the content is in that language.
Overall, thank you for sharing the knowledge.
Great blog.
Cheers,
Henrique.
--------------------------------------------------
Thank you Henrique! Each tip is very valid. In future posts I will try to make this clearer.
Dear Bruno,
Thankyou for sharing the information. Very useful.
Regards,
Venkat
Thanks Venkateswaran!
I'm still adapting to the new scn and I'm moving blogs and soon would like to share more.
Regards
Bruno Xavier.