function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
pavan.elthepu1.392651269495206E12pavan.elthepu1.392651269495206E12 

Failed to Load Attachment

Hi,

From Javascript, I'm trying to get attachment body and saving as attachment in Salesforce. But I'm getting Failed to load error on opening attachment. Here is my code:
 
jQuery.ajax({
	type: "GET",
	url: "https://c.cs66.content.force.com/services/data/v39.0/sobjects/Attachment/00P0v000000IlLh/Body",
	beforeSend: function (xhr) {
		xhr.responseType = "arraybuffer";
		xhr.setRequestHeader('Authorization', "OAuth " + session);
		//usually not needed but when you are
		//xhr.setRequestHeader('Content-Type', 'application/pdf');
		xhr.setRequestHeader('Access-Control-Allow-Origin', '*');
		xhr.setRequestHeader('Access-Control-Allow-Methods', 'GET');
	},
	success: function (data) {
		var att = new sforce.SObject("Attachment");
		att.Name = "Pavan_JS.pdf";

		att.ContentType = 'application/pdf';
		att.Body = att.Body =   (new sforce.Base64Binary(data)).toString();
		//att.Body = window.btoa(data); - Works for txt file, but not for pdf files
		att.ParentId ="0030v000001cnSB";
		var result = sforce.connection.create([att]);	
	}
});

Note: This code is working gret for txt files, but not for pdf files. Please help me