No Response from Frappe Call, when uploading file (using uploadfile() )

Hello,
I wanted to make a private file in system to public file in order to access its public URL and send to WhatsApp through API integration.
For this I tried to use upload file method to get the public URL of file but did not receive response from frappe.call.

This is my code:

function publicdocument(url, doctype, docname) {
console.log(“url”,url,“doctype”,doctype,“docname”, docname);
var blob = null;
var xhr = new XMLHttpRequest();
xhr.open(“GET”, url);
var splittedParts = url.split("/");
var splitByQuestionMark = splittedParts[5].split(’?’);
var filename = splitByQuestionMark[0];
console.log(splitByQuestionMark);
console.log(“file name”,filename,“doctype”,doctype,“docname”,docname);

        xhr.responseType = "blob"; //force the HTTP response, response-type header to be blob
        xhr.onload = function() {
            blob = xhr.response; //xhr.response is now a blob object
            console.log("blob",blob);
            var reader = new FileReader();
            console.log("File Reader",reader);
            reader.readAsDataURL(blob);
            reader.onloadend = function() {
                var base64data = reader.result;
                console.log("ye base64data mila", base64data);
                console.log("file name",filename,"doctype",doctype,"docname",docname);
                frappe.call({
                    type: "POST",
                    method: "uploadfile",
                    args: {
                        doctype:doctype,
                        docname:docname,
                        filename:"myfile.pdf",
                        filedata:base64data,
                        is_private:0
                        // is_public:1
                    },
                    async: false,
                    success: function(r) {
                        // console.log("helppppppoppssojihusfad")
                        //frappe.msgprint(r)
                        // console.log("file name",filename,"doctype",doctype,"docname",docname);
                        console.log("Callback response",r);
                        // frappe.call
                    },
                    error: function(data) {
                        console.log("Inside Error",data);
                    }
                    
                });
            };
        };
        xhr.send();
    }

Please let me know what changes should I make to my code.
Thank you.

Hi, just browsing this intesting topic.

I didn’t really know why your code didn’t work.
But to achive what you said you want to achive, I would suggest you can make an API call in the backend (assume you have access to backend if you self host your ERP instance), so that it will make a file from private to public, which you will able to know the public URL. (Check how file Doctype works in ERPNext). Then the backend API can make a HTTP request directly to your Whatsapp integration.

If my assumption is wrong or you would like to make the flow works in clien side, I will suggest you use debugger in browser to check which part might be failed, as I see that you have used too much console.log. (Not saying that’s not right, but use the browser debugger tool, you would be able to set any break point to check step by step)