Search code examples
imageodatasapui5

How do I pass an Image in the body of a OData Request in SAPUI5?


I need to pass an sap.m.Image file to the body(Data) of an OData request. Below is the code and I would like to know what to pass to the data parameter of the request so that my Image gets uploaded to the backend. When I pass the ImgValue which contains the dataurl it gives out an error saying

DOMException: Failed to execute 'createElementNS' on 'Document': The qualified name provided ('d:0') contains the invalid name-start character

OData.request({
    requestUri: "http://ambrifiori.am.brothergroup.net:8081/sap/opu/odata/sap/ZPVSYSTEM_SRV/PromoImagesSet/",
    method: "POST",

    headers: {
        "X-Requested-With": "XMLHttpRequest",
        "Content-Type": "application/atom+xml",
        "DataServiceVersion": "2.0",
        /*"Accept": "application/atom+xml,application/atomsvc+xml,application/xml",  */
        "X-CSRF-Token": header_xcsrf_token,
        "slug": "ajay122",
    },
    data: ImgValue,
});

Solution

  • I wasn't able to post image data through OData hence I used ajax... This is what I did.

                    OData.request 
                ({  
                    requestUri:      "http://AMBRIFIORI.am.brothergroup.net:8081/sap/opu/odata/sap/ZUI5_DAILY_SALES_SRV/DailySalesSet",  
                    method: "GET",  
                    headers:  
                    {       
                        "X-Requested-With": "XMLHttpRequest", 
                        "Content-Type": "application/atom+xml", 
                        "DataServiceVersion": "2.0",          
                        "X-CSRF-Token":"Fetch"                                 }                    
                },  
                function (data, response) 
                { 
                    header_xcsrf_token = response.headers['x-csrf-token'];       
                    csrftoken = header_xcsrf_token;
    
                    $.ajax({
                          url: 'http://ambrifiori.am.brothergroup.net:8081/sap/opu/odata/sap/ZPVSYSTEM_SRV/PromoImagesSet/',
                          //dataType: 'json',
                          data: imgData,
                          //data: image,
                          type: 'POST',
                            headers: {   "X-Requested-With": "XMLHttpRequest",                        
                                "Content-Type": "image/png", 
                                "DataServiceVersion": "2.0",  
                                /*"Accept": "application/atom+xml,application/atomsvc+xml,application/xml",  */
                                "X-CSRF-Token": csrftoken, 
                                "slug": slug,
                                },                    
                          success: function(data) {
                              debugger;
                            console.log(data);
                            },
                                error: function(data) {
                                    debugger;
                                    console.log(data);
                                }
                          });                   
    

    My ImgData consists of image in Data URI format base64. I just added one statement in my Imgvalue to convert it to ImgData which is

                    var imgData = JSON.stringify(ImgValue);