Search code examples
javascriptuploadsapui5

is it possible to convert uploadcollection's object into a xstring?


Using the sapui5 uploadcollection to upload files in the frontend and then sending them through ajax with a post request...

I need to know how to convert te returned object from the uploadcollection control into a xstring, so then I can send that xstring (that contains the file content) To a sap gateway by using ajax post method.

Any idea how could I do this?

Right now I'm sending files by using the uploadcollection, once I upload an attachment, the control returns an object that represents the file content.

I'm trying to make this object a xstring by using filereader:

                                                        //obtiene archivo
                                                        var file = files[i];

                                                        //Convierte archivo en binario
                                                        var reader = new FileReader();
                                                        reader.onload = function(readerEvt) {

                                                            var binaryString = readerEvt.target.result;
                                                            var base64 = btoa(binaryString);
                                                            var base64file; 

                                                            if(typeof base64file == "undefined" || typeof base64file == null){
                                                                base64file = base64; 
                                                            }else{
                                                              base64file = base64file +'new'+base64;
                                                            }
                                                        };
                                                        reader.readAsBinaryString(file);

                                                        console.log(file)

But this work only with files of type image, the others like pdf, .doc etc etc give the following error when I try to send them with ajax.

"The Data Services Request could not be understood due to malformed syntax".

Any idea how can I send convert these files into a xstring data?


Solution

  • I figured it out by filling an array everytime that a file was uploaded through the control,

                        change: function(oEvent) {
    
    
                            //Get file content
                            file = oEvent.getParameter("files")[0];
    
                            //Prepare data for slug
                            fixname = file.name;
                            filename = fixname.substring(0, fixname.indexOf("."));
                            extension = fixname.substring(fixname.indexOf(".") + 1);
    
                            //fill array with uploaded file
                            var fileData = {
                                file: file,
                                filename: filename,
                                extension: extension
                            }
                            fileArray.push(fileData);
    
                        },
    

    and then I did a loop over that array to post every single file I keept there by using ajax method post.

                                                    $.each(fileArray, function(j, valor) {
    
                                                        //get file
                                                        file = fileArray[j].file;
    
                                                        //get file lenght
                                                        var numfiles = fileArray.length;
    
    
                                                        //Convert file to binary
                                                        var reader = new FileReader();
                                                        reader.readAsArrayBuffer(file); 
                                                        reader.onload = function(evt) {
                                                            fileString = evt.target.result; 
    
                                                            //get and make slug
                                                            filename = fileArray[j].filename;
                                                            extension = fileArray[j].extension;
                                                            slug = documento + '/' + filename + '/' + extension;
    
    
                                                            //User url service
                                                            var sUrlUpload = "sap url";
                                                            runs++;
    
                                                            //Post files
                                                            jQuery.ajax({});
                                                        }
                                                    });