Skip to Content

Preview Documents .docx, png using GET_STREAM OData

Hi Experts

My requirement is to preview any document attached on PO's and i have implemented GET_STREAM on the OData. I am able to preview .PDF documents however no other document types will preview. They are immediately downloaded which is not what i want.

    CALL FUNCTION 'SO_DOCUMENT_READ_API1'
      EXPORTING
        document_id                = lv_docid
      IMPORTING
        document_data              = ls_document_data
      TABLES
        contents_hex               = lt_hex_content
      EXCEPTIONS
        document_id_not_exist      = 1
        operation_no_authorization = 2
        x_error                    = 3
        OTHERS                     = 4.
    
    IF sy-subrc = 0.
      IF lt_hex_content IS NOT INITIAL.
*...set streaming
        ls_stream-mime_type = |application/| && |{ ls_document_data-obj_type }|.

        LOOP AT lt_hex_content ASSIGNING FIELD-SYMBOL(<ls_hex_content>).
          IF ls_stream-value IS INITIAL.
            ls_stream-value = <ls_hex_content>-line.
          ELSE.
            ls_stream-value = ls_stream-value && <ls_hex_content>-line.
          ENDIF.
        ENDLOOP.

        copy_data_to_ref( EXPORTING is_data = ls_stream
                          CHANGING  cr_data = er_stream ).

*...Download or Preview the document
        ls_header-name = 'content-disposition'.
        ls_header-value = 'inline; filename="' && ls_document_data-obj_descr &&
                          |.| && |{ ls_document_data-obj_type }| && |";|.   "preview
        set_header( is_header = ls_header ).

      ENDIF.
    ENDIF.

PDF documents will preview in the browser but other types will download immediately yet i want to preview in the browser.

regards

Prince Isaac

Add a comment
10|10000 characters needed characters exceeded

Assigned Tags

Related questions

1 Answer

  • Best Answer
    Posted on May 19 at 02:57 PM

    Update:

    I have managed to get some document types to preview but one in particular .DOC still downloads immediately. Changes i had to make was to retrieve the MIME_TYPE from table SDOKFEXT using the file extension of the document.

    *...set media type
            ls_stream-mime_type = me->get_mime_type( |{ ls_document_data-obj_type CASE = LOWER }| ).
    
            LOOP AT lt_hex_content ASSIGNING FIELD-SYMBOL(<ls_hex_content>).
              IF ls_stream-value IS INITIAL.
                ls_stream-value = <ls_hex_content>-line.
              ELSE.
                ls_stream-value = ls_stream-value && <ls_hex_content>-line.
              ENDIF.
            ENDLOOP.
    
            copy_data_to_ref( EXPORTING is_data = ls_stream
                              CHANGING  cr_data = er_stream ).
    
    *...Download or Preview the document
            ls_header-name = 'content-disposition'.
            ls_header-value = 'inline; filename="' && ls_document_data-obj_descr &&
                              |.| && |{ ls_document_data-obj_type }| && |";|.   "preview
            set_header( is_header = ls_header ).
    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.