In the SAP Note 3280607 - WDA: Multiple File Upload, the SAP suggests using an SAP UI5 component to upload multiple files in the Webdynpro application:
The WebDynpro FileUpload control does not support multiple file upload, but only one file at a time.
In higher SAP_UI releases (SAP_UI 7.54 onwards) the solution suggestion would be to embed the SAP UI5 control UploadSet into a WebDynpro HTML Island or alternatively into a WebDynpro iFrame control.
In this example, we will create a simple WebDynpro ABAP component called ZWDA_UI5_FILEUPLOADER to show how to embed the SAP UI5 control into a WebDynpro HTML Island.
name | data element | category | length |
FILENAME_WITH_FILETYPE | STRING | ||
MIMETYPE | W3CONTTYPE | char | 128 |
SIZE | STRING | ||
CONTENT | XSTRING |
Column | Cell Editor | property bind |
COL_FILEDOWNLOAD | FileDownload | (See the next picture) |
COL_MIMETYPE | TextView | MAIN.FILES_UPLOADED.MIMETYPE |
COL_SIZE | TextView | MAIN.FILES_UPLOADED.SIZE |
ID ID_CSA
name id
value sap-ui-bootstrap
ID LIB_CSA
name data-sap-ui-libs
value sap.m
var MyFileUploader = MyFileUploader || {
init: function (oCallbackApi, maxFilenameLength, maxFileSize, fileType) {
if (fileType !== null) {
var filetypeArray = fileType.split(",");
} else {
filetypeArray = null
}
this.oCallbackApi = oCallbackApi;
this.oFileUploader = new sap.m.UploadCollection({
maximumFilenameLength: parseInt(maxFilenameLength),
maximumFileSize: parseInt(maxFileSize), // Specifies a file size limit in megabytes
multiple: true,
sameFilenameAllowed: false,
instantUpload: false,
fileType: filetypeArray,
change: this.onChange,
typeMissmatch: this.ontypeMissmatch,
fileSizeExceed: this.onfileSizeExceed,
filenameLengthExceed: this.onfilenameLengthExceed,
});
this.oFileUploader.placeAt("FileUploadUI5");
},
onChange: function (oControlEvent) {
for (let index = 0; index < oControlEvent.mParameters["files"].length; index++) {
const file = oControlEvent.mParameters["files"][index];
MyFileUploader.uploadToCallbackAPI(file);
}
},
ontypeMissmatch: function (oControlEvent) {
let file = oControlEvent.mParameters["files"][0];
MyFileUploader.oCallbackApi.fireEvent('UI5TypeMismatch', '{"filename":"' + file.name + '",' + '"filetype":"' + file.fileType + '"}');
},
onfilenameLengthExceed: function (oControlEvent) {
let file = oControlEvent.mParameters["files"][0];
MyFileUploader.oCallbackApi.fireEvent('UI5NameLenghtExceed', file.name );
},
onfileSizeExceed: function (oControlEvent) {
let file = oControlEvent.mParameters["files"][0];
MyFileUploader.oCallbackApi.fireEvent('UI5SizeExceed', file.name );
},
uploadToCallbackAPI(file) {
if (file == null) {
return;
}
var oFileReader = new FileReader();
var fileName = file.name.split(".")[0];
var fileType = file.name.split(".")[1];
var mimetype = file.type;
var size = file.size;
oFileReader.onload = function (evt) {
var raw = evt.target.result;
var hexString = MyFileUploader.convertBinaryToHex(raw).toUpperCase();
var fileAsJsonString = MyFileUploader.createJsonObjectForFileInfo(fileName, fileType, mimetype, size, hexString);
MyFileUploader.oCallbackApi.fireEvent('UI5Upload', fileAsJsonString);
};
oFileReader.onerror = function (evt) {
sap.m.MessageToast.show("error");
};
oFileReader.readAsArrayBuffer(file);
},
createJsonObjectForFileInfo: function (fileName, fileType, mimetype, size, hexString) {
return '{"filename":"' + fileName + '",' +
'"filetype":"' + fileType + '",' +
'"mimetype":"' + mimetype + '",' +
'"size":"' + size + '",' +
'"hexcont":"' + hexString + '"}';
},
convertBinaryToHex: function (buffer) {
return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
},
removeFirstFile: function (evt) {
var oFirstFileItem = MyFileUploader.oFileUploader.getItems()[0];
MyFileUploader.oFileUploader.removeItem(oFirstFileItem);
}
}
METHOD wddomodifyview .
CONSTANTS:
c_file_upload_html_id TYPE string VALUE 'FileUploadUI5',
c_html_insland_id TYPE string VALUE 'HTML_ISLAND_FILE_UPLOADER_UI5',
c_ui5_core_script_id TYPE string VALUE 'UI5_CORE_SCRIPT'.
IF first_time = abap_true.
" use central UI5-version provided by WDA
DATA(lv_script_string) = cl_wd_utilities=>get_ui5_root_path( ).
IF lv_script_string IS NOT INITIAL.
CONCATENATE lv_script_string 'sap-ui-core.js' INTO lv_script_string.
CONDENSE lv_script_string NO-GAPS.
CAST cl_wd_html_script( view->get_element( id = c_ui5_core_script_id ) )->set_source( value = lv_script_string ).
ENDIF.
" set static html
DATA(lv_static_html) = |<div id="{ c_file_upload_html_id }" style="height:100%"></div>|.
wd_this->mo_html_island = CAST cl_wd_html_island( view->get_element( c_html_insland_id ) ).
wd_this->mo_html_island->set_static_html( lv_static_html ).
" Call the Init Javascript function and pass the Webdynpro callback API to allow the JavaScript code call Webdynpro Actions
wd_this->mo_html_island->add_script_call( cl_wd_html_script_call=>new_call(
)->variable( `MyFileUploader`
)->function( `init`
)->add_callback_api(
)->add_string( '100' " Maximum Filename Length
)->add_string( '1' " Maximum FileSize (Specifies a file size limit in megabytes)
)->add_string( 'jpg,pdf,txt,xls,xlsx,png' " FileTypes (use comma as separator)
* )->add_null( " Use add_null if all the file types should be allowed
) ).
ELSEIF wd_this->mv_remove_file = abap_on.
wd_this->mo_html_island->add_script_call( cl_wd_html_script_call=>new_call(
)->variable( `MyFileUploader`
)->function( `removeFirstFile` ) ).
wd_this->mv_remove_file = abap_off.
ENDIF.
ENDMETHOD.
METHOD onactionui5_upload .
TYPES:
BEGIN OF ty_file_uploaded,
filename TYPE string,
filetype TYPE string,
mimetype TYPE string,
size TYPE string,
hexcont TYPE string,
END OF ty_file_uploaded.
DATA ls_file_uploaded_js TYPE ty_file_uploaded.
DATA ls_files_uploaded_wd TYPE wd_this->element_files_uploaded.
DATA:
BEGIN OF ls_file_size,
value TYPE k_bytes,
unit TYPE cacs_byte_unit,
END OF ls_file_size.
IF wdevent IS NOT BOUND.
RETURN.
ENDIF.
DATA(lv_file_upload_json) = wdevent->get_string( `DATA` ).
/ui2/cl_json=>deserialize( EXPORTING json = lv_file_upload_json
CHANGING data = ls_file_uploaded_js ).
ls_files_uploaded_wd-mimetype = ls_file_uploaded_js-mimetype.
ls_files_uploaded_wd-content = ls_file_uploaded_js-hexcont.
ls_files_uploaded_wd-filename_with_filetype = |{ ls_file_uploaded_js-filename }.{ ls_file_uploaded_js-filetype }|.
CALL FUNCTION 'CACS_CONVERT_BYTE'
EXPORTING
i_byte = CONV k_bytes( ls_file_uploaded_js-size )
IMPORTING
e_value = ls_file_size-value
e_unit = ls_file_size-unit.
ls_files_uploaded_wd-size = |{ ls_file_size-value } { ls_file_size-unit }|.
DATA(lo_nd_files_uploaded) = wd_context->get_child_node( wd_this->wdctx_files_uploaded ).
lo_nd_files_uploaded->bind_structure( new_item = ls_files_uploaded_wd
set_initial_elements = abap_false ).
wd_this->mv_remove_file = abap_on.
ENDMETHOD.
METHOD onactionui5_file_size_exceed .
DATA(lv_filename) = wdevent->get_string( `DATA` ).
wd_this->wd_get_api( )->get_message_manager( )->report_error_message(
EXPORTING
message_text = |The File "{ lv_filename }" exceeded the size limit| ).
ENDMETHOD.
METHOD onactionui5_name_leng_exceed .
DATA(lv_filename) = wdevent->get_string( `DATA` ).
wd_this->wd_get_api( )->get_message_manager( )->report_error_message(
EXPORTING
message_text = |"{ lv_filename }" file name is longer than allowed| ).
ENDMETHOD.
method ONACTIONUI5_TYPE_MISSMATCH .
TYPES:
BEGIN OF ty_file_uploaded,
filename TYPE string,
filetype TYPE string,
END OF ty_file_uploaded.
DATA ls_file_uploaded_js TYPE ty_file_uploaded.
IF wdevent IS NOT BOUND.
RETURN.
ENDIF.
DATA(lv_file_upload_json) = wdevent->get_string( `DATA` ).
/ui2/cl_json=>deserialize( EXPORTING json = lv_file_upload_json
CHANGING data = ls_file_uploaded_js ).
wd_this->wd_get_api( )->get_message_manager( )->report_error_message(
EXPORTING
message_text = |The File type { ls_file_uploaded_js-filetype } from the file { ls_file_uploaded_js-filename } is not allowed| ).
endmethod.
ID HTML_EVT_UI5_UPLOAD
name UI5Upload
Events / onAction UI5_UPLOAD
ID HTML_EVT_UI5_SIZE_EXCEED
name UI5SizeExceed
Events / onAction UI5_FILE_SIZE_EXCEED
ID HTML_EVT_UI5_NAME_LENG_EXCEED
name UI5NameLenghtExceed
Events / onAction UI5_NAME_LENG_EXCEED
ID HTML_EVT_UI5_TYPE_MISMATCH
name UI5TypeMismatch
Events / onAction UI5_TYPE_MISSMATCH
" Call the Init Javascript function and pass the Webdynpro callback API to allow the JavaScript code call Webdynpro Actions
wd_this->mo_html_island->add_script_call( cl_wd_html_script_call=>new_call(
)->variable( `MyFileUploader`
)->function( `init`
)->add_callback_api(
)->add_string( '100' " Maximum Filename Length
)->add_string( '1' " Maximum FileSize (Specifies a file size limit in megabytes)
)->add_string( 'jpg,pdf,txt,xls,xlsx,png' " FileTypes (use comma as separator)
* )->add_null( " Use add_null if all the file types should be allowed
) ).
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
10 | |
8 | |
7 | |
6 | |
6 | |
6 | |
5 | |
4 | |
3 | |
3 |