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.