Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Attachment is uploading uisng GlideSysAttachment but error while downloading

riyachuphal
Tera Contributor

I have tried uploading my img using GlideSysAttachment() into my record and it uploads but when i try to view it - it shows file format not  supported. Please help me understand the fault in uploading process.
Here's my code for writing data into content variable:

 

upFile.addEventListener('change', attachFiles, false);
function attachFiles(evt){
	files = evt.target.files;
	for (var i = 0; i < files.length; i++) {
        var file = files[i];
        filename = file.name;
        contentType = file.type;
		var fileR = new FileReader();
        fileR.onload = (function(file) {
            return function(e) {
                content = e.target.result;
            };
        })(file);
		fileR.readAsDataURL(file);
	}
	document.getElementById("submit").disabled = false;
}

 

 Then a glideajax call to call my script include:

 

function onSubmit() 
{
	GlideDialogWindow.get().destroy();
	alert(content);
	var retestga = new GlideAjax("Myscripts");
		retestga.addParam("sysparm_name", "uploadRetestAttachments1");
		retestga.addParam("filename", filename);//filename);
		retestga.addParam("contenttype", contentType);//contentType);
		retestga.addParam("img_content", content);//window.btoa(content));
		retestga.addParam("rec_sysid", '0871******************');
		return true;
}

 

and here's the script include :

 

uploadRetestAttachments1 : function(){ 
		gs.addErrorMessage('hehhhhhhh');
		var fileName = this.getParameter('filename'); 
		var contentType = this.getParameter('contenttype'); 
		var content = this.getParameter('img_content');
		var itemSysId = this.getParameter('rec_sysid');
		gs.addInfoMessage('Processing file:  hello- '+fileName +" - "+contentType+content);
		if(typeof content =='string'){
			var data =content.split(',')[0];
			var data1 = content.split(',')[1];
			gs.addInfoMessage(data);
			gs.addInfoMessage(data1);
		}
		var attachment = new GlideSysAttachment();
		var rec = new GlideRecord("retest_table");
		rec.get(itemSysId);  
		var resp = attachment.write(rec, fileName, contentType, data1);
		gs.addInfoMessage('resp-----'+resp);
	return true;
},

 


I am able to see my encoded string correctly and if I try to decode it using some external online decoders- it works .

Here's the encoded string for reference:
iVBORw0KGgoAAAANSUhEUgAAAAEAAAADCAYAAABS3WWCAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAFiUAABYlAUlSJPAAAAASSURBVBhXY/gPBEwMQAAkGBgAMfsEARoEDOwAAAAASUVORK5CYII=

Actual pic is just one small white dot and my error on download:

error_fileformatnotsupported.png

 

0 REPLIES 0